0

Im using a System.IO.BinaryReader to read a data from a serialized file. The file is big endian. So what I did to read the Int64 (long) from the stream was read 8 bytes from it. What I need to do is convert those 8 bytes of big endian into little endian long representation.

I can't use System.BitConverter as it will be compiled with Mono for use on Linux which could be running on big endian and with Visual Studio for Windows which is little endian.

Cole Tobin
  • 9,206
  • 15
  • 49
  • 74

3 Answers3

3

You can use System.Net.IPAddress.NetworkToHostOrder or System.Net.IPAddress.HostToNetworkOrder

For ex, System.Net.IPAddress.HostToNetworkOrder(0x0123456789abcdefl) would return efcdab8967452301

L.B
  • 114,136
  • 19
  • 178
  • 224
  • 1
    Don't forget that the machine you are on might be big endian in the first place. You can check by using BitConverter.IsLittleEndian http://msdn.microsoft.com/en-us/library/system.bitconverter.islittleendian.aspx . – Will Apr 23 '12 at 00:35
1

If you're going to end up using Mono anyway, you can use DataConvert.

Works fine on Windows too. I'm using it in one of my projects.

mpen
  • 272,448
  • 266
  • 850
  • 1,236
1

John Skeet's miscutil library implements a BigEndianBitConverter.

spender
  • 117,338
  • 33
  • 229
  • 351
  • I opened up his library and discovered he uses `Operator` in GenericMath. Never knew about this... I could have used it many times. Very cool! – mpen Apr 24 '12 at 19:57