1

I have a binary file written in big endian enviorment using c code. er On my PC a C# application is reading bytes from the file using BinaryReader and converting it to Managed struct using Marshalling.

the sturuct contains a few float fields.

I'm getting the starngest result.. When running in the Visual studio I get the floats with a ~10^-6 accuracy , but, When I run the exe file. Some of the float field are not accurate (~10^-3).

Anyone encountered this issue?

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
TheDude
  • 257
  • 1
  • 9
  • 1
    Can you give some sample values (byte array and expected converted result)? – Jon Skeet Nov 25 '13 at 06:59
  • There should be no concern of "accuracy" - Provided you're displaying them on both sides with enough decimal places, the values should be *identical*. – Jonathon Reinhart Nov 25 '13 at 07:00
  • This shows you how accurate the floats are. http://stackoverflow.com/questions/753948/why-is-floating-point-arithmetic-in-c-sharp-imprecise – deathismyfriend Nov 25 '13 at 07:00
  • @deathismyfriend Most likely unrelated. – Jonathon Reinhart Nov 25 '13 at 07:01
  • @Jon Skeet -0.5178069 on visual studio and -0.5187835 on the exe. the input is new Byte[] { 255,142,4,191,224 } – TheDude Nov 25 '13 at 07:32
  • The thing is the diffrence between the numbers is one bit! the input is FF-8E-04-BF and the exe translate it to FF-CE-04-BF. – TheDude Nov 25 '13 at 07:41
  • It is correct that `BitConverter.ToSingle(new byte[] { 0xFF, 0x8E, 0x04, 0xBF, }, 0).ToString("R")` gives `"-0.517806947"` while `BitConverter.ToSingle(new byte[] { 0xFF, 0xCE, 0x04, 0xBF, }, 0).ToString("R")` gives `"-0.5187835"`. That is not very surprising. The interesting part is how did `8E` get converted to `CE`? That is impossible for us to answer with the information you have given. Maybe "something" thinks this is [ISO 8859-1](http://en.wikipedia.org/wiki/ISO/IEC_8859-1) encoded text. The octet `8E` is invalid in that **encoding**, maybe "something" just adds `40` in that case? – Jeppe Stig Nielsen Nov 25 '13 at 14:06

0 Answers0