[C++][代码共享]将UTF-8的std::string转换成std::wstring

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值