EDITED: 我正在用C ++编写一个函数,它应该使用十六进制字符串,并且应该使用c ++将其转换为unicode字符串。我使用流进行的转换没有给出所需的输出。你能建议一个解决方案吗?
如果我把它作为硬编码的unicode(比如“e820”到“\ u00e8 \ u0020”)然后工作,但如何在运行时制作它?
示例1:
示例2:
我的代码示例:
//Hex string that I have
std::string str( "6880d7a5876b2b9b" );
std::string Temp( "\u00e8\u0020\u00b1\u009b\u007a\u00e5\u0006\u00ff" );
std::string result;
//Take 2 bytes and convert it to ascii
for (int i = 0; i < (int)str.size(); i += 2)
{
std::istringstream iss(str.substr(i, 2));
int temp;
iss >> std::hex >> temp;
result += static_cast<char>(temp);
}
std::cout << result << std::endl;
std::cout << Temp << std::endl;
经过几周的努力,我能够使用boost lib找到解决方案: 修复如下:
std::string HexString("6880d7a5876b2b9b"); //Hex string as input
std::string UnicodeString;
//Take a char i.e 2 chars in hex string
for (int i = 0; i < ( int )HexString.size(); i += 2)
{
//Convert to int from hex
std::istringstream iss( HexString.substr( i, 2 ) );
unsigned int temp;
iss >> std::hex >> temp;
//Convert to \uxxx unicode
std::string Utf8Char = boost::locale::conv::utf_to_utf<char>( &temp, (&temp + 1) );
UnicodeString.append( Utf8Char );
}
std::cout<<"UnicodeString:"<<UnicodeString<<std::endl