std::wstring utf_to_wstr(const std::string& s)
{
const char* str_src = s.c_str(); //源字符串首地址
wchar_t *str_dst = new wchar_t[4096]; //文字数组
unsigned char* cur_byte = (unsigned char*)str_src; //后面涉及到与数字比较,使用unsigned
int wchar_idx = 0;
while (*cur_byte != '\0' && (void*)cur_byte <= (void*)(str_src + strlen(str_src)))
{
if (*cur_byte < 0x80) //1 byte
{
str_dst[wchar_idx] = cur_byte[0];
cur_byte++;
}else if(0xC0 <= *cur_byte && *cur_byte < 0xE0) { //2 bytes
str_dst[wchar_idx] = ((cur_byte[0] & 0x1F) << 6) | (cur_byte[1] & 0x3F);
cur_byte += 2;
}else if(0xE0 <= *cur_byte && *cur_byte < 0xF0) { //3 bytes,此方法返回的wchar最大只能用到3byte的UTF-8字符
str_dst[wchar_idx] = ((cur_byte[0] & 0xF) << 12) | ((cur_byte[1] & 0x3F) << 6) | (cur_byte[2] & 0x3F);
cur_byte += 3;
}else{ //无效字符
str_dst[wchar_idx] = '.';
cur_byte++;
}
wchar_idx++;
}
str_dst[wchar_idx] = '\0'; //字符串结束符
std::wstring ret = str_dst;
delete str_dst;
return ret;
}
[C++][代码共享]将UTF-8的std::string转换成std::wstring
最新推荐文章于 2023-06-13 16:25:12 发布