我们使用 typedef 将数据类型 byte_pointer 定义为一个指向类型为 unsigned char 的对象的指针。
#include <iostream>
//#include <iomanip>
using namespace std;
typedef unsigned char *byte_pointer;
void show_byte(byte_pointer start,int len)
{
int i;
cout << "16进制:";
for (i = 0;i < len;i++)
cout << hex << (int)start[i] << " ";// 默认以十六进制(hex)输出
cout << endl;
cout << "10进制:";
for (i = 0;i < len;i++)
cout << dec << (int)start[i] << " ";//以十进制(dec)输出
cout << endl << i << endl;
}
void show_int(int x)
{
show_byte((byte_pointer)&x,sizeof(int));//强制类型转换,无论指针&x以前是什么类型,现在都是char*类型的指针
}
void show_long_long_int(int x)
{
show_byte((byte_pointer)&x,sizeof(long long int));
}
int main()
{
cout << "系统转换:" << hex << 1234589 << endl;
int m = 12345;
show_int(m);
long long int n = 123456789;
show_long_long_int(n);
return 0;
}