How are real numbers represented in binary form?
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 rea开发者_高级运维l number?
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.
精彩评论