1、16进制编码转化为字符编码
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
// 待输出的字符串(16进制编码表示)
char sztext[1024] = "\x63\x2B\x2B\xCD\xF8\xC2\xE7\xB1\xE0\xB3\xCC\xA3\xA1";
int nRead = strlen(sztext);
// 存储还原后的字符串
char sz2[1024] = "";
for (int i = 0;i < nRead;++i)
{
// 将16进制编码还原字符串
if(sztext[i] < 0)
sz2[i] = sztext[i] - 256;
else
sz2[i] = sztext[i];
}
// 输出还原后的字符串
printf("%s\n",sz2);
return 0;
}
2、字符编码转化为16进制编码
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char sztext[1024] = "c++网络编程!";//
int nRead = strlen(sztext);
char sz2[1024] = "";
for (int i = 0;i < nRead;++i)
{
int nind = 0;
// 将字符编码转转换为16进制编码
if(sztext[i] < 0)
nind = sztext[i] + 256;
else
nind = sztext[i];
char szTemp[10]="";
// 16进制用小写表示,格式化串用“%02x”
// 16进制用大写表示,格式化串用“%02X”
sprintf(szTemp,"%02X ",nind);
strcat(sz2,szTemp);
}
printf("%s\n",sz2);
return 0;
}