文字说明部分,可借鉴http://blog.youkuaiyun.com/hdutigerkin/article/details/7663454
本文给出一段例程,来验证常用的char,short,int,float,double的详细存储过程。
#include <string>
#include <Windows.h>
template<class T>
void StypePrint(T &stype,int ntype)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),12);
printf("The type of sample : %s:\n",typeid(T).name());
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),10);
if (!strcmp(typeid(T).name(),"float")||!strcmp(typeid(T).name(),"double"))
printf("Addr=%8X Value=%.2lf\n",&stype,*(&stype),stype);
else
printf("Addr=%8X Value=%8d\n",&stype,*(&stype),stype);
char binary[53];//double整数位最多0-51,,+‘\0’
_itoa_s(stype,binary,2);
printf("Addr=%8X ASCII=%8c IntVl=%35s\n",binary,*binary,binary);//itoa函数只能输出整数部分二进制
char* buff = (char*) (&stype);
for(int i=0;i<ntype;i++)
{
printf("Addr=%8X ASCII=%8c Value=%35d\n",&buff[i],buff[i],buff[i]);
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),11);
if (!strcmp(typeid(T).name(),"float")||!strcmp(typeid(T).name(),"double"))
printf("Value=%.2lf\nTOTAL:",stype);
else
printf("Value=%8d\nTOTAL: ",stype);
for(int i=ntype-1;i>=0;i--)
{
char nbinary[33];
_itoa_s(buff[i],nbinary,2);
if (strlen(nbinary)>8)//为负数
{
printf(" ");
for(int j=strlen(nbinary)-8;j<=strlen(nbinary);j++)//strlen长度计算原理是搜索到‘\0’
printf("%c",nbinary[j]);//取后8位输出
}
else
printf("%8s",nbinary);//为正数
}
printf("\n");
}
int main()
{
char scha=125;
StypePrint(scha,1);
short sshort=125;
StypePrint(sshort,2);
int sint=125;
StypePrint(sint,4);
float sfloat=125.5;
StypePrint(sfloat,4);
double sdouble=125.5;
StypePrint(sdouble,8);
system("pause");
return 0;
}
/*
任何数据在内存中都是以二进制的形式存储的,例如一个short型数据1156,其二进制表示形式为00000100 10000100。则在Intel CPU架构的系统中,存放方式为 10000100(低地址单元) 00000100(高地址单元),因为Intel CPU的架构是小端模式。
//
