wxString and '\0' Topic is solved
wxString and '\0'
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 *
At the end I should convert that content to char *
valodas - free cross-platform language learning software
http://www.valodas.com
http://www.valodas.com
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
- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
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.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
valodas - free cross-platform language learning software
http://www.valodas.com
http://www.valodas.com
OK, my mistake, you can append to wxString '\0' symbol, but you can't do such:
Why?
Code: Select all
wxString c = _T("\x00");
int len = c.Length(); // len is 0
valodas - free cross-platform language learning software
http://www.valodas.com
http://www.valodas.com
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?
output:
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());
}
However, even if it's possible, i personally think a string should not be used to store random binary data.11:50:20: 'wx' (2)
11:50:20: 'wx' (2)
11:50:20: 'wx' (5)
11:50:20: '' (1)
Use the source, Luke!