//定义的short类型,每个short类型,占2个字节
short data[10] = { 0 };
data[0] = 0x0030; //十进制的48
data[1] = 0x0031; //十进制的49
data[2] = 0x0032; //十进制的50
data[3] = 0x33; //实际为0033
data[4] = 0x34; //实际为0034
data[5] = 0x35; //实际为0035
data[6] = 0x36; //实际为0036
data[7] = 0x37; //实际为0037
data[8] = 0x38; //实际为0038
data[9] = 0x39; //实际为0039
//short类型,定义short类型的指针
short *shP = data;
// 十进制显示 48
cout << "data[0] ==" << *shP << endl;
//每个次++,都跨过2个字节
shP++;
// 十进制显示 49
cout << "data[1] ==" << *shP << endl;
cout << "---转成char---" << endl;
通过指针来转换,就是解引用时,需要多少内存空间。shart需要2个字节,char只需要1个字节。
char * ch = (char *)data;
// 十进制是48,字符显示0
cout << "char ==" << *ch << endl;
ch++;
//跳过中间都是0的字符
ch++;
//十进制是49,字符显示1
cout << "char ==" << *ch << endl;
cout << "转成int" << endl;
int * intp = (int*)data;
// int是4个字节,short是2个字节,所以取的值是:00310030(16进制) 3211312(10进制)
cout << "int ==" << *intp << endl;
intp++;
// int是4个字节,short是2个字节,所以取的值是:00330032(16进制) 3342386(10进制)
cout << "int ==" << *intp << endl;
//4
cout << "sizeof(int) ==" << sizeof(int) << endl;
//2
cout << "sizeof(short) ==" << sizeof(short) << endl;
通过这一系列的转换,对于底层的数据存储,有了更深的认识。
网友评论