Reply:
Yes, I tried no problem
Reply:
# Import
# Include
# Include
int main (int argc, const char * argv []) {
std :: wstring sz = L "abc123 you and me";
wprintf (L "% s \ n", sz.c_str ());
NSLog (@ "% s \ n", sz.c_str ());
return 0;
}
-------------------
The code above outputs:
a
2012-11-14 09:13:22.932 test [572:303] a
--------------------
Also whether 64 or 32, in? Xcode under sizeof (wchar_t) is 4, while? Windows under 2.
UNICODE is not mac system is UTF32, rather than UTF16?
Reply:
Unix-wchar_t implementation is UTF32, mac os x darwin so the bottom is also UTF32
Also can not use c_str () indicates unicode, encounters 0 on the end
wprintf, wcout like the c function seems to unicode support is limited, do not quite understand
Can be converted into NSString output:
std :: wstring wstr = L "abc123 you and me";
NSData * data = [NSData dataWithBytes: wstr.data () length: sizeof (wchar_t) * wstr.size ()];
NSString * str = [[NSString alloc] initWithData: data encoding: NSUTF32LittleEndianStringEncoding];
NSLog (@ "str% @", str);
Reply:
# Include
# Import
class CStringConverter {
public:
static std :: wstring Ansi2Unicode (std :: string szAnsi) {
NSString * szNs = [NSString stringWithCString: szAnsi.c_str ()];
return Ns2Unicode (szNs);
}
static std :: string Unicode2Ansi (std :: wstring szUnicode) {
NSString * szNs = Unicode2Ns (szUnicode);
const char * rs = [szNs cString];
return rs;
}
static std :: wstring Utf82Unicode (std :: string szUtf8) {
NSString * szNs = [NSString stringWithUTF8String: szUtf8.c_str ()];
return Ns2Unicode (szNs);
}
static std :: string Unicode2Utf8 (std :: wstring szUnicode) {
NSString * szNs = Unicode2Ns (szUnicode);
return [szNs UTF8String];
}
static std :: string Ansi2Utf8 (std :: string szAnsi) {
return Unicode2Utf8 (Ansi2Unicode (szAnsi));
}
static std :: string Utf82Ansi (std :: string szUtf8) {
return Unicode2Ansi (Utf82Unicode (szUtf8));
}
static std :: wstring Ns2Unicode (NSString * szNs) {
if (! szNs) return L ""; / / note: wchar_t is utf32 under mac
const char * szUtf8 = [szNs UTF8String];
setlocale (LC_ALL, "");
/ / Calc block size to be returned
int len = mbstowcs (0, szUtf8, 0);
/ / Malloc and fill the returned block
wchar_t * szUnicode = new wchar_t [len + 1];
mbstowcs (szUnicode, szUtf8, len);
szUnicode [len] = 0;
std :: wstring rs = szUnicode;
delete [] szUnicode;
return rs;
}
/ / Return value is autorelease, besure do not need to invoke NSString's release
static NSString * Unicode2Ns (std :: wstring szUnicode) {
setlocale (LC_CTYPE, "");
/ / Calc block size to be returned
int len = wcstombs (0, szUnicode.c_str (), 0);
/ / Malloc and fill the returned block
char * szUtf8 = new char [len + 1];
wcstombs (szUtf8, szUnicode.c_str (), len);
szUtf8 [len] = 0;
NSString * rs = [NSString stringWithUTF8String: szUtf8];
delete [] szUtf8;
return rs;
}
};
std :: string szUtf8 = "abc123 you and me";
std :: wstring szUnicode = CStringConverter :: Utf82Unicode (szUtf8);
wprintf (szUnicode.c_str ());
printf ("\ n");
std :: wstring sz = L "abc123 you and me";
wprintf (sz.c_str ());
printf ("\ n");
=================
The first printed out, and the second print out, and why?
Reply:
While the first one can be printed, but in fact there is not szUnicode UTF32, look at its contents:
<61000000 62000000 63000000 31000000 32000000 33000000 e4000000 bd000000 a0000000 e6000000 88000000 91000000 e4000000 bb000000 96000000>
abc123 is no problem, but "you and me" these three words encoding is UTF8 encoding of each byte extended to 32, so szUnicode stuff inside is actually "abc123 you and me" corresponds to the UTF8 encoding wide character instead UTF32
From that perspective wprintf and printf, can only handle UTF8-encoded characters, can not handle UTF32
No comments:
Post a Comment