
c语言中没有可以直接打印float类型数据的二进制或者十六进制编码的输出格式,
因此,需要单独给个函数,如下:
unsigned int float2hexRepr(float* a){
unsigned int c;
c= ((unsigned int*)a)[0];
return c;
} int main(int argc, char const *argv[])
{
printf("%s\n", "== in float representation == ");
float f1 = 15213.0;
printf("%x\n", float2hexRepr(&f1));
}
结果如下:
== in float representation ==
466db400
为了更好看,打印出二进制:
void hex2binaryStr(unsigned int x, char* str){
unsigned int xCopy = x;
for (int i = ; i < ; ++i)
{
str[ - i] = (xCopy & )? '': '';
xCopy = xCopy >> ;
}
} void printBinary(char* str){
for (int i = ; i < ; ++i)
{
printf("%c", str[i]);
if (((+i)% == ) && ((+i) != ))
{
printf("%c", ',');
}
}
printf("\n");
}
结果如下:
== in float representation ==
466db400
,,,,,,,
THE END