[size=1em][size=1em]include <iostream>
[size=1em]using namespace std;
[size=1em]typedef unsigned char byte;
[size=1em]// 转换char(视为整数类型)为16进制字符串
[size=1em]void ChtoHex(byte Val, char* dest)
[size=1em]{
[size=1em] // 辗转相除法,倒序看得到结果
[size=1em] byte tmp = Val % 16;
[size=1em] if (tmp >= 0 && tmp <= 9)
[size=1em] {
[size=1em] dest[1] = '0' + tmp;
[size=1em] }
[size=1em] else if (tmp >= 10 && tmp <= 15)
[size=1em] {
[size=1em] dest[1] = 'A' + tmp - 10;
[size=1em] }
[size=1em] tmp = (Val/16) % 16;
[size=1em] if (tmp >= 0 && tmp <= 9)
[size=1em] {
[size=1em] dest[0] = '0' + tmp;
[size=1em] }
[size=1em] else if (tmp >= 10 && tmp <= 15)
[size=1em] {
[size=1em] dest[0] = 'A' + tmp - 10;
[size=1em] }
[size=1em] // 设置'\0'
[size=1em] dest[2] = '\0';
[size=1em]}
[size=1em]// 主函数
[size=1em]int main()
[size=1em]{
[size=1em] int u = 367328153; // 原始数据,8位16进制为15 E4 FB 99
[size=1em] byte a, b, c, d; // u从低地址到高地址的四个字节
[size=1em] // a~d对应的16进制字符串,预留3个字符
[size=1em] char Sa[3], Sb[3], Sc[3], Sd[3];
[size=1em] byte* k = (byte*)&u;
[size=1em] a = k[0];
[size=1em] b = k[1];
[size=1em] c = k[2];
[size=1em] d = k[3];
[size=1em] // 转成16进制字符串
[size=1em] ChtoHex(a, Sa);
[size=1em] ChtoHex(b, Sb);
[size=1em] ChtoHex(c, Sc);
[size=1em] ChtoHex(d, Sd);
[size=1em] cout << Sa << " " << Sb << " " << Sc << " " << Sd << endl;
[size=1em] system("pause");
[size=1em] return 0;
[size=1em]}