UTF-8与std::string转换

string stringUTF8(const string& str)
{
    int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
    wchar_t* pwBuf = new wchar_t[nwLen + 1];
    ZeroMemory(pwBuf, nwLen * 2 + 2);
    ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen);
    int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
    char* pBuf = new char[nLen + 1];
    ZeroMemory(pBuf, nLen + 1);
    ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);
    std::string retStr(pBuf);
    delete[]pwBuf;
    delete[]pBuf;
    pwBuf = NULL;
    pBuf = NULL;
    return retStr;
}
 
string UTF8string(string strTemp)
{
    char buf[1024 * 60];
    snprintf(buf, sizeof(buf), u8"%s", strTemp.c_str());
    TCHAR wscBuffer[1024 * 10] = { 0 };
    MultiByteToWideChar(CP_UTF8, 0, buf, (int)strlen(buf) + 1, wscBuffer, sizeof(wscBuffer) / sizeof(wchar_t));
    memset(buf, 0, 1024 * 9);
    WideCharToMultiByte(CP_ACP, 0, wscBuffer, -1, buf, 1024 * 9, NULL, NULL);
    return buf;
}

C++中string的UTF-8格式_三石目的博客-CSDN博客_c++ string utf8

补充:wstring 和 string 区别

wstring是宽字符,占用2个字节的大小,针对UNICODE编码格式,用于对中文汉字的定义和赋值。wstring跟string区别为:字节不同、编码格式不同、使用不同。

一、字节不同
1、wstring:wstring是宽字符,占用2个字节的大小,即16bit。

2、string:string是窄字符,占用1个字节的大小,即8bit。

也就是说,宽字符,每表示一个字符其实是占了16bit,即2个char的大小。而汉字就是需要16bit来表示。

二、编码格式不同
1、wstring:wstring一般针对UNICODE编码格式,一个单元两个char。

2、string:string一般针对ASCII编码格式,一个单元一个char。

三、使用不同
1、wstring:在使用中文汉字时,使用wstring来定义变量进行赋值。

2、string:在使用英文汉字时,使用string来定义变量进行赋值。


https://blog.csdn.net/liuming690452074/article/details/115765683

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值