版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liangzhao_jay/article/details/86703101
背景: 因需要对比含有汉字字符串是否是 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;
}