How to get the IEEE 754 binary representation of a float in C#

HasaniH picture HasaniH · Nov 22, 2010 · Viewed 14.6k times · Source

I have some single and double precision floats that I want to write to and read from a byte[]. Is there anything in .Net I can use to convert them to and from their 32 and 64 bit IEEE 754 representations?

Answer

Hans Passant picture Hans Passant · Nov 22, 2010

.NET Single and Double are already in IEEE-754 format. You can use BitConverter.ToSingle() and ToDouble() to convert byte[] to floating point, GetBytes() to go the other way around.