32位数字的十六进制重新presentations [英] Hexadecimal representations of 32-bit numbers

查看:132
本文介绍了32位数字的十六进制重新presentations的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个生成两个随机的32位浮点数a和b,并把他们作出输出C的脚本。

I have a script that generates two random 32-bit floating point numbers a and b, and divides them to make an output c.

我想存储在十六进制格式,这些浮点数的所有3个,为包含8个字符的字符串。

I would like to store all 3 of these floating point numbers in hexadecimal format, as a string that contains 8 characters.

我发现了一个聪明的办法做到这一点在这里在线:

I found a clever way to do this online here:

http://forums.devshed.com/c-programming-42/printing-a-float-as-a-hex-number-567826.html

这是我的执行其在C ++

And here's my implementation of their advice in C++

    fileout << hex << *(int*)&a[i] << endl;
    fileout << hex << *(int*)&b[i] << endl;
    fileout << hex << *(int*)&c[i] << endl;

这适用于大多数情况下。然而,有些情况下,字符串是不宽为8个字符。有时他们是唯一一个有点长。这里是输出的示例:

This works for most of the cases. However, some of the cases, the strings are not 8 characters wide. Sometimes they are only one bit long. Here is the sample of the output:

                            af1fe786
                    ffbbff0b
                    fffbff0b
                    7fbcbf00  <-I like it, but has zeros at the end 
                    77fefe77
                    7ffcbf00
                    fdad974d
                    f2fc7fef
                    4a2fff56
                    67de7744
                    fdf7711b
                    a9662905
                    cd7adf0   <-- problem
                    5f79ffc0
                    0         <--- problem
                    6ebbc784
                    cffffb83
                    de3bcacf
                    e7b3de77
                    ec7f660b
                    3ab44ae4
                    aefdef82
                    fffa9fd6
                    fd1ff7d2
                    62f4      <--why not "62f40000"
                    ebbf0fa6
                    ddd78b8d
                    4d62ebb3
                    ff5bbceb
                    3dfc3f61
                    ff800000 <- zeros at end, but still 8 bytes?
                    df35b371
                    e0ff7bf1
                    3db6115d
                    fbbfbccc
                    ddf69e06
                    5d470843
                    a3bdae71
                    fe3fff66
                    0         <--problem
                    979e5ba1
                    febbe3b9
                    0         <-problem
                    fdf73a80
                    efcf77a7
                    4d9887fd
                    cafdfb07
                    bf7f3f35
                    4afebadd
                    bffdee35
                    efb79f7f
                    fb1028c   <--problem

我要8个字符重新presentations。至于零的情况下,我想将其转换为00000000。

I want 8-character representations. As for the case of zero, I want to convert it to "00000000".

不过,我真搞不清楚这是只有4,5,6,7个字符长的人。一些数字,为什么会得到填充零末和其他被截断?如果int是32位,为什么有时只有一个位露面?这是因为臭名昭著的低于正常的数字?

But I am really confused about the ones that are only 4, 5, 6, 7 characters long. Why do some numbers get zero filled at the end and others get truncated? If an int is 32 bits, why does sometimes only one bit show up? Is this due infamous "subnormal" numbers?

感谢。

推荐答案

如果我理解正确的话您的要求,您要添加的零作为填充字符到的的你的十六进制重新$ P $的psentations如果的位数小于8

If I understand correctly your requirement, you want to add zeros as a filling character to the left of your hexadecimal representations if the number of digits is less than 8.

在这种情况下,你可以简单地使用 的std :: setfill() 上的 的std ::运输及工务局局长() 流操纵:

In that case, you can simply use the std::setfill() the std::setw() stream manipulators:

#include <iomanip> // Necessary for the setw and setfill

int n = ...;
std::cout << std::hex << std::setw(8) << std::setfill('0') << n;

有关 N = 1024 ,例如,输出将是:

For n = 1024, for instance, the output will be:

00000400    

这篇关于32位数字的十六进制重新presentations的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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