在C++中,将string转换为UTF-8编码的字符串可以通过以下方法实现:
std::wstring_convert
和std::codecvt_utf8
库(C++11及更高版本)。#include<iostream>
#include<string>
#include<locale>
#include <codecvt>
std::string convertToUTF8(const std::string& input) {
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
std::wstring wideString = converter.from_bytes(input);
return converter.to_bytes(wideString);
}
int main() {
std::string input = "你好,世界!";
std::string utf8String = convertToUTF8(input);
std::cout << "UTF-8 string: "<< utf8String<< std::endl;
return 0;
}
iconv
或Boost.Locale
。这里是一个使用iconv
库的示例:
#include<iostream>
#include<string>
#include <iconv.h>
std::string convertToUTF8(const std::string& input) {
iconv_t cd = iconv_open("UTF-8", "UTF-8"); // 根据需要更改源和目标编码
if (cd == (iconv_t)-1) {
std::cerr << "Error opening iconv"<< std::endl;
return "";
}
size_t inSize = input.size();
char* inBuf = const_cast<char*>(input.data());
size_t outSize = inSize * 4; // 预留足够的空间
char* outBuf = new char[outSize];
char* outPtr = outBuf;
while (inSize > 0) {
if (iconv(cd, &inBuf, &inSize, &outPtr, &outSize) == (size_t)-1) {
std::cerr << "Error converting string to UTF-8"<< std::endl;
break;
}
}
iconv_close(cd);
std::string result(outBuf, outPtr - outBuf);
delete[] outBuf;
return result;
}
int main() {
std::string input = "你好,世界!";
std::string utf8String = convertToUTF8(input);
std::cout << "UTF-8 string: "<< utf8String<< std::endl;
return 0;
}
请注意,这些示例仅适用于已经是UTF-8编码的字符串。如果输入字符串使用其他编码(如GBK、GB2312等),则需要相应地调整iconv_open
函数中的参数。