c++ string、UTF8相互转换方法

 普通sting类型 转UTF-8编码格式字符串
std::string ofDewarServer::string_To_UTF8(const std::string & str)
{
int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);

wchar_t * pwBuf = new wchar_t[nwLen + 1];//一定要加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;
}
//


UTF-8编码格式字符串  转普通sting类型

std::string ofDewarServer::UTF8_To_string(const std::string & str)
{
int nwLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);

wchar_t * pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然会出现尾巴
memset(pwBuf, 0, nwLen * 2 + 2);

MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), pwBuf, nwLen);

int nLen = WideCharToMultiByte(CP_ACP, 0, pwBuf, -1, NULL, NULL, NULL, NULL);

char * pBuf = new char[nLen + 1];
memset(pBuf, 0, nLen + 1);

WideCharToMultiByte(CP_ACP, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);

std::string retStr = pBuf;

delete []pBuf;
delete []pwBuf;

pBuf = NULL;
pwBuf = NULL;

return retStr;
}
//



  • 11
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
### 回答1: C++中的std::string本质上是一个字符数组,它不包含字符编码信息。如果要将std::string换为UTF-8编码的字符串,需要先确定std::string中存储的字符编码,然后进行相应的换。 假设std::string中存储的是ASCII编码的字符串,可以直接将其换为UTF-8编码的字符串,方法如下: ```c++ #include <string> #include <iostream> int main() { std::string str = "Hello, world!"; std::string utf8str; utf8str.reserve(str.size()); // 将ASCII编码的字符换为UTF-8编码的字符 for (char c : str) { if (c < 0x80) { utf8str.push_back(c); } else { utf8str.push_back(0xc0 | (c >> 6)); utf8str.push_back(0x80 | (c & 0x3f)); } } std::cout << utf8str << std::endl; return 0; } ``` 如果std::string中存储的是其他字符编码,例如GB2312,需要先将其换为Unicode编码,然后再将Unicode编码换为UTF-8编码。可以使用第三方库,例如iconv库,进行编码换。 ### 回答2: 在C语言中,将一个字符串换为UTF-8编码需要使用一些字符处理的函数和方法。以下是一个简单的示例: ```c #include <stdio.h> #include <stdlib.h> #include <locale.h> #include <wchar.h> #include <string.h> int main() { setlocale(LC_ALL, ""); // 设置本地环境以支持宽字符输出 char* utf8Str = "你好,世界!"; // 假设要换的字符串为UTF-8编码 wchar_t* wideStr = (wchar_t*)malloc(sizeof(wchar_t) * (strlen(utf8Str) + 1)); mbstowcs(wideStr, utf8Str, strlen(utf8Str) + 1); // 将UTF-8字符串换为宽字符字符串 wprintf(L"Wide String: %ls\n", wideStr); // 输出宽字符字符串 free(wideStr); return 0; } ``` 以上代码中,我们先使用`setlocale`函数设置本地环境以支持宽字符输出。然后,我们声明一个UTF-8编码的字符串`utf8Str`。接下来,我们使用`mbstowcs`函数将UTF-8字符串换为宽字符字符串`wideStr`,并分配了足够的内存。最后,我们使用`wprintf`函数输出宽字符字符串。 请注意,换为UTF-8是根据输入字符串的编码格式而定的。如果输入字符串不是UTF-8编码,需要使用不同的方法来进行换。 ### 回答3: 将C++字符串换为UTF-8格式需要以下步骤: 1. 确保你的C++编译器支持UTF-8编码。 2. 在程序中引入相关的库,如:`<locale>`和`<codecvt>`。 3. 定义一个辅助函数,用于将C++字符串换为UTF-8格式: ```cpp string convertToUTF8(const wstring& str) { wstring_convert<codecvt_utf8<wchar_t>> converter; return converter.to_bytes(str); } ``` 4. 使用上述函数将C++字符串换为UTF-8格式: ```cpp string input = "你好"; wstring wideInput(input.begin(), input.end()); string utf8Input = convertToUTF8(wideInput); ``` 在上述代码中,我们首先将C++字符串换为宽字符串(`wstring`),然后调用辅助函数将宽字符串换为UTF-8格式的字符串。 请注意,以上方法适用于C++11及更高版本。如果你使用的是旧版本的C++,则可能需要其他方式来进行换。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值