Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/liangzhao_jay/article/details/86703101
Background: Due need to compare whether the kanji character string containing a UTF-8 encoding or other encoding, so this write procedure
Sample code:
#include<iostream>
#include<string>
#include<sstream>
#include<iomanip>
using namespace std;
int main()
{
string temp("ab中国");
cout<<"源字符串 : "<<temp<<endl;
stringstream out;
for(string::const_iterator it != temp.begin(); it != temp.end(); ++it)
{
out<<std::hex<<setw(3)<<(static_cast<short>(*it) & 0xff);
}
cout<<"16进制字符串:"<<out.str();
return 0;
}