背景: 因需要对比含有汉字字符串是否是 UTF-8编码 或者 其他编码,故编写此程序
示例代码:
#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;
}