在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
函數中的參數。