为什么同时使用MultiByteToWideChar和WideCharToMultiByte?
我看到一些这样的代码: 为什么同时使用MultiByteToWideChar和WideCharToMultiByte?
char szLine[MAX_LENGTH_STRING] = {0}
... //some operate to szLine
char *szUtf8string;
wchar_t *szUnicodeString;
int size;
int room;
size = strlen(szLine)+1;
room = MultiByteToWideChar(CP_ACP, 0, szLine, -1, NULL, 0);
szUnicodeString = (wchar_t*) malloc((sizeof(wchar_t))*room);
MultiByteToWideChar(CP_ACP, 0, szLine, -1, szUnicodeString, room);
room = WideCharToMultiByte(CP_UTF8, 0, szUnicodeString, -1, NULL, 0, NULL, NULL);
szUtf8string = (char*) malloc(room);
WideCharToMultiByte(CP_UTF8, 0, szUnicodeString, -1, szUtf8string, room, NULL, NULL);
I saw some code like this:
Why use MultiByteToWideChar and WideCharToMultiByte at the same time?
char szLine[MAX_LENGTH_STRING] = {0}
... //some operate to szLine
char *szUtf8string;
wchar_t *szUnicodeString;
int size;
int room;
size = strlen(szLine)+1;
room = MultiByteToWideChar(CP_ACP, 0, szLine, -1, NULL, 0);
szUnicodeString = (wchar_t*) malloc((sizeof(wchar_t))*room);
MultiByteToWideChar(CP_ACP, 0, szLine, -1, szUnicodeString, room);
room = WideCharToMultiByte(CP_UTF8, 0, szUnicodeString, -1, NULL, 0, NULL, NULL);
szUtf8string = (char*) malloc(room);
WideCharToMultiByte(CP_UTF8, 0, szUnicodeString, -1, szUtf8string, room, NULL, NULL);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此代码片段首先将字符串从使用系统默认代码页的多字节表示形式转换为 Unicode,然后将其转换为 UTF-8 多字节表示形式。因此,它将默认代码页中的文本转换为 UTF-8 表示形式。
该代码很脆弱,因为它假设 UTF-8 版本的大小只会加倍(这可能在大多数情况下都有效,但更糟糕的情况是默认代码页中的单个字节可能映射到 UTF-8 中的 4 个字节) 8).
This code fragment first converts the string from the a multibyte representation using the system default code page to Unicode, then converts it to the UTF-8 multibyte representation. Thus, it converts text in the default code page to UTF-8 representation.
The code is fragile, in that it assumes the UTF-8 version will only double in size (this probably works most of the time, but the worse case is that a single byte in the default code page may map to 4 bytes in UTF-8).