C++ with wxWidgets, Unicode vs. ASCII, what's the difference?
I have a Code::Blocks 10.05 rev 0 and gcc 4.5.2 Linux/unicode 64bit and WxWidgets version 2.8.12.0-0
I have a simple problem:#define _TT(x) wxT(x)
string file_procstatus;
file_procstatus.assign("/PATH/TO/FILE");
printf("%s",file_procstatus.c_str());
wxLogVerbose(_TT("%s"),file_procstatus.c_str());
Printf outputs "/PATH/TO/FILE" normally while wxLogVerbose turns into crap. When I want to change std::string to wxString I have to开发者_如何学编程 do following:
wxString buf;
buf = wxString::From8BitData(file_procstatus.c_str());
Somebody has an idea what might be wrong, why do I need to change from 8bit data?
This is to do with how the character data is stored in memory. Using the "string" you produce a string of type char
using the ASCII character set whereas I would assume that the _TT
macro expands to L"string" which create a string of type wchar_t
using a Unicode character set (UTF-32 on Linux I believe).
the printf
function is expecting a char
string whereas wxLogVerbose
I assume is expecting a wchar_t
string. This is where the need for conversion comes from. ASCII used one byte per character (8 bit data) but wchar_t
strings use multiple bytes per character so the problem is down to the character encoding.
If you don't want to have to call this conversion function then do something like the following:
wstring file_procstatus = wxT("/PATH/TO/FILE");
wxLogVerbose(_TT("%s"),file_procstatus.c_str());
The following article gives best explanation about differences in Unicode and ASCII character set, how they are stored in memory and how string functions work with them.
http://allaboutcharactersets.blogspot.in/
精彩评论