954,202 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Big Endian float conversion

Hi,
I am trying to convert a Big Endian data file to something readable on most Windows PCs. I do ok with integer data but I cannot convert the floating points (32-bit IEEE).
1) Is it possible for a BinaryReader to account for Big Endian, such that .ReadSingle() would return the correct value?

2) If I have to read the bytes one at a time and reverse them, is there a way to type cast it to a float?

I pursued #2 for a while, but C# type-casting seems to convert the actual value and not the binary that represents it. So the result is something like 1.12E9 instead of 89.9.
I really don't know how to make it work correctly. I would be grateful for any help.

Regards,
Piote

piote
Newbie Poster
3 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

1) Yeah, you can tell the BinaryReader what encoding to use with the System.Text.Encoding class.

BinaryReader reader = new BinaryReader( stream, Encoding.BigEndianUnicode );

2) No, but you can use the System.BitConverter class to turn an array of bytes into whatever type it supports. And it supports both single and double precision floating point conversions. :)

Hamrick
Posting Whiz
325 posts since Jun 2007
Reputation Points: 180
Solved Threads: 34
 

Thanks for your help!

Regards,
Piote

piote
Newbie Poster
3 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

And how do I convert it the other way round.

Regards,
Sidharth Shah.

sidharthrshah
Newbie Poster
3 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You