场景:
1.有些库需要接收utf8字符串(也比如数据库表数据),而路径处理则需要unicode字符串,所有在windows下互相转换是必须的。
2.之前自己实现了utf8转unicode,但是并不完美,因为超过4字节的utf8字符串或big endian的字节序支持不好,所以在windows最好的方式应该就是使用系统接口了.
#include "windows.h"
char* QXUtf82Unicode(const char* utf, size_t *unicode_number)
{
if(!utf || !strlen(utf))
{
*unicode_number = 0;
return NULL;
}
int dwUnicodeLen = MultiByteToWideChar(CP_UTF8,0,utf,-1,NULL,0);
size_t num = dwUnicodeLen*sizeof(wchar_t);
wchar_t *pwText = (wchar_t*)malloc(num);
memset(pwText,0,num);
MultiByteToWideChar(CP_UTF8,0,utf,-1,pwText,dwUnicodeLen);
*unicode_number = dwUnicodeLen - 1;
return (char*)pwText;
}
char* QXUnicode2Utf8(const char* unicode)
{
int len;
len = WideCharToMultiByte(CP_UTF8,
本文介绍了在Windows环境下,由于某些库需要UTF8字符串,而路径处理需要Unicode字符串,因此需要进行两者之间的转换。作者指出,之前实现的UTF8转Unicode转换存在对于超过4字节UTF8字符串及Big Endian字节序支持不足的问题,并推荐使用系统接口进行更完善的转换。
订阅专栏 解锁全文
9493





