如何把4字节浮点数float转换成十六进制

时间:2021-06-17 17:24:57
如何把4字节浮点数float转换成十六进制
例如:float型100 转换为十六进制 42c80000

6 个解决方案

#1


自己顶下,急呀....帮忙呀!

#2


sprintf(Buffer,"%x",float_Number);

#3


不过楼主的要求有点奇怪,通常只是在前面补0,而不是在后面,否则这个数就不是100了,要清楚float当中的位序.否则的话,按楼主的意思,只能是单字节转换.

#4


int num = 100;
char buff[9] = {0};
sprintf(buff, "%08x", num);

However, the standard c/c++ doesn't support the input of float type for converting hexadecimal. if you want do it, you may need to write the converting function yourself, by the principle from the float to binary.

#5


float float_number=100;
char Buffer[2*4+1];
sprintf(Buffer,
"%2.2x%2.2x%2.2x%2.2x",
*(((unsigned char*)&float_number)+3),
*(((unsigned char*)&float_number)+2),
*(((unsigned char*)&float_number)+1),
*(((unsigned char*)&float_number)+0));

#6


float x=100; //   42c80000
   unsigned int y;
   y= *(unsigned int *)(&x);
   printf("%08x",y);

#1


自己顶下,急呀....帮忙呀!

#2


sprintf(Buffer,"%x",float_Number);

#3


不过楼主的要求有点奇怪,通常只是在前面补0,而不是在后面,否则这个数就不是100了,要清楚float当中的位序.否则的话,按楼主的意思,只能是单字节转换.

#4


int num = 100;
char buff[9] = {0};
sprintf(buff, "%08x", num);

However, the standard c/c++ doesn't support the input of float type for converting hexadecimal. if you want do it, you may need to write the converting function yourself, by the principle from the float to binary.

#5


float float_number=100;
char Buffer[2*4+1];
sprintf(Buffer,
"%2.2x%2.2x%2.2x%2.2x",
*(((unsigned char*)&float_number)+3),
*(((unsigned char*)&float_number)+2),
*(((unsigned char*)&float_number)+1),
*(((unsigned char*)&float_number)+0));

#6


float x=100; //   42c80000
   unsigned int y;
   y= *(unsigned int *)(&x);
   printf("%08x",y);