如何在C中以十六进制字节格式打印浮点数? [英] how to print float as hex bytes format in C?

查看:568
本文介绍了如何在C中以十六进制字节格式打印浮点数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想查看IEEE754格式表示形式中的浮点值3.14159265.所以写了这个测试代码.

I wanted to see the float value 3.14159265 in IEEE754 format representation. So wrote this test code.

#include <stdio.h>

main()
{
int i;
float a = 3.141592654;

printf("a = %f\n", a);

// print test 1
printf("test 1 : a = %x\n", a);
printf("test 1 2nd time : a = %x\n", a);

// print test 2
char *pt = (char *)&a;
printf("test 2 :\n");
for(i=0;i<4;i++){
    printf("%x ", (char)(*pt++));
}
printf("\n");
}

我多次运行它时,它显示如下:

When I run it, several times, it shows like this :

ckim@stph45:~/test5] test
a = 3.141593
test 1 : a = e0cd2000
test 1 2nd time : a = e0cd2000
test 2 :
ffffffdb f 49 40 
ckim@stph45:~/test5] test
a = 3.141593
test 1 : a = 520db000
test 1 2nd time : a = 520db000
test 2 :
ffffffdb f 49 40 
ckim@stph45:~/test5] test
a = 3.141593
test 1 : a = 3b373000
test 1 2nd time : a = 3b373000
test 2 :
ffffffdb f 49 40 
ckim@stph45:~/test5] test
a = 3.141593
test 1 : a = 18a6d000
test 1 2nd time : a = 18a6d000
test 2 :
ffffffdb f 49 40 

第一个问题是:当将printf与%x一起使用时,我预期会看到40490fdb,它是IEEE754格式的3.14159265.但是正如您所见,每次我运行它时,值都会改变.这是怎么了也许我缺少一些非常基本的东西.

First question is : when using printf with %x, I expected to see 40490fdb which is 3.14159265 in IEEE754 format. But the value changes everytime I run it as you can see. What's wrong with this here? Maybe I'm missing something very basic.

另一个问题是,当我像上面的测试2一样使用字符指针并打印十六进制值时,如何打印"db"而不是"fffffdb"? (该值显示为已扩展符号).我想我以前做过这些事情,但现在已经不记得了. (我的机器是低位字节序,因此首先显示LSB.)

Anoother question is, when I use character pointer and print the hex values as in test 2 above, how can I print 'db' instead of 'fffffdb'? (the value is shown sign extened). I think I've done these things before but can't remember it now. (My machine is little endian so LSB is shown first.)

推荐答案

您缺少的是如何在构成IEEE-754单精度浮点的位上获得有效的 look 数字作为其在内存中的等效unsigned int表示形式.为此,在避免违反 strict别名规则(键入punning指针)的同时,可以在floatunsigned之间使用union(对于精确宽度类型,最好使用uint32_t),例如

What you are missing is how to obtain a valid look at the bits that make up the IEEE-754 single-precision floating point number as its equivalent unsigned int representation in memory. To do that while avoiding violating the strict aliasing rule (type punning pointers), you can use a union between float and unsigned (or better uint32_t for exact width type), e.g.

#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <math.h>

int main (void) {

    float pi = (float)M_PI;
    union {
        float f;
        uint32_t u;
    } f2u = { .f = pi };

    printf ("pi : %f\n   : 0x%" PRIx32 "\n", pi, f2u.u);

    return 0;
}

使用unsigned而不是uint32_t并了解并非在所有硬件上都是32位,因此标准 format字符串就足够了,例如printf ("pi : %f\n : 0x%0x\n", pi, f2u.u);. (在这种情况下,您可以删除stdint.hinttypes.h的包含内容)

Using unsigned instead of uint32_t and understanding it may not be 32-bits on all hardware, the standard format string will suffice, e.g. printf ("pi : %f\n : 0x%0x\n", pi, f2u.u);. (you can drop the include of stdint.h and inttypes.h in that case)

使用/输出示例

当解释为无符号类型时,这将允许您获取组成浮点数的位的十六进制表示形式:

This will allow you to get the hex representation of the bits that make up the floating-point number when interpreted as an unsigned type:

$ ./bin/float_hex
pi : 3.141593
   : 0x40490fdb

这篇关于如何在C中以十六进制字节格式打印浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆