wxString and '\0' Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
normunds
Knows some wx things
Knows some wx things
Posts: 31
Joined: Mon Sep 03, 2007 8:14 am
Contact:

wxString and '\0'

Post by normunds »

Is it possible to save '\0' character into wxString? If not, what is preferred way to save zero values into string? wxChar[], wxCharBuffer, char[] or other?

At the end I should convert that content to char *
valodas - free cross-platform language learning software
http://www.valodas.com
Grrr
Earned some good credits
Earned some good credits
Posts: 126
Joined: Fri Apr 11, 2008 8:48 am
Location: Netherlands

Post by Grrr »

wxString is for C style 0-terminated strings. I think storing '\0' will cause problems. Maybe wxMemoryBuffer is a better alternative?
Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:

Post by Jorg »

You can store any character in a wxString. You can even read a file to it, write it back later without any problems (in ANSI mode). But if you use the c_str() conversion method that exports the buffer of a wxString as a const char * and you send it to a normal c style method, it will stop reading the string when the first \0 is encountered.

- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
normunds
Knows some wx things
Knows some wx things
Posts: 31
Joined: Mon Sep 03, 2007 8:14 am
Contact:

Post by normunds »

Jorg wrote:You can store any character in a wxString. You can even read a file to it, write it back later without any problems (in ANSI mode). But if you use the c_str() conversion method that exports the buffer of a wxString as a const char * and you send it to a normal c style method, it will stop reading the string when the first \0 is encountered.

- Jorgen
Actually I was trying to append '\0' char with += and Append methods without result. That all in Unicode mode. But if there is no possibility to export that string to char* then I will look for another way, where to store this string.
valodas - free cross-platform language learning software
http://www.valodas.com
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

normunds wrote:But if there is no possibility to export that string to char* then I will look for another way, where to store this string.
If what you're trying to do is convert a wxString to a C-style char*, just use mb_str() method
normunds
Knows some wx things
Knows some wx things
Posts: 31
Joined: Mon Sep 03, 2007 8:14 am
Contact:

Post by normunds »

OK, my mistake, you can append to wxString '\0' symbol, but you can't do such:

Code: Select all

wxString c = _T("\x00");
int len = c.Length(); // len is 0
Why?
valodas - free cross-platform language learning software
http://www.valodas.com
User avatar
doublemax
Moderator
Moderator
Posts: 19161
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax »

if you want to create a string with 0-bytes, you must pass the number of chars to the ctor. How else should the code know where your string ends?

Code: Select all

{
	wxChar teststring1[]=wxT("wx");
	wxString s1(teststring1);
	wxLogMessage(wxT("'%s' (%d)"), s1, s1.Len());

	wxChar teststring2[]=wxT("wx\x00wx");
	wxString s2(teststring2);
	wxLogMessage(wxT("'%s' (%d)"), s2, s2.Len());

	wxChar teststring3[]=wxT("wx\x00wx");
	wxString s3(teststring3, 5);
	wxLogMessage(wxT("'%s' (%d)"), s3, s3.Len());

	wxChar teststring4[]=wxT("\x00");
	wxString s4(teststring4, 1);
	wxLogMessage(wxT("'%s' (%d)"), s4, s4.Len());
}
output:
11:50:20: 'wx' (2)
11:50:20: 'wx' (2)
11:50:20: 'wx' (5)
11:50:20: '' (1)
However, even if it's possible, i personally think a string should not be used to store random binary data.
Use the source, Luke!
Post Reply