实数如何以二进制形式表示?
我需要在二进制文件中存储一个包含小数位(即 1.5)的数字,问题是当我想读取此文件时,我不知道如何将字节转换回数字,我知道对于整数我只需要这样做:byte[0] << 24 |字节[1] << 16 | 16字节[2] << 8 | byte[3]
表示大端形式的 Int32。对于实数,该怎么做呢?
I need to store a number that contains decimal places (ie 1.5) inside a binary file, the problem is that I don't know how to convert the bytes back to a number when I want to read this file, I know that for integers I only have to do this: byte[0] << 24 | byte[1] << 16 | byte[2] << 8 | byte[3]
for an Int32 in big endian form. What would be the way to do that for a real number?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看 BitConverter 类。它包含将各种基本类型与字节数组相互转换的方法。这样你就不需要知道浮点数是如何用二进制表示的。
如果您确实想知道这一点,我认为关于 floating-point 的维基百科文章是一个好地方了解它。
Have a look at the BitConverter class. It contains methods to convert the various basic types to and from byte arrays. That way you don't need to know how the floating-point numbers are represented in binary.
If you do want to know that, I think the wikipedia article on floating-point is a good place to learn about it.
您无法将实数存储为位。告诉计算机该数字是有符号还是无符号的数据还告诉它它支持什么级别的子整数精度。计算机做一个简单的除法问题。
You can't store real numbers as bits. The data that tells the computer if the number is signed or unsigned also tells it what level of sub-integer precision it supports. The computer does a simple division problem.