Click to See Complete Forum and Search --> : how to open file and read as little-endian or big-endian


jasonlimvm
October 30th, 2009, 05:12 AM
Hi,

Can anyone tell me how to open a binary file and read as either little or big endian using c#.
Any website or sample code that i can refer to???


Thanks in advance.

DrBob
October 30th, 2009, 11:37 AM
I think you will have to manage it by yourself. You could read the file into a byte[] then convert it to whatever you need; short[] or integer[].

This method produce an integer from a byte array that contains an integer formatted in big endian. The method assumes a 4 bytes array.

public int ConvertByteArrayToInteger(byte[] bInteger)
{
if (BitConverter.IsLittleEndian == true)
Array.Reverse(bInteger);

return BitConverter.ToInt32(bInteger, 0);
}

DrBob
October 30th, 2009, 11:40 AM
I forgot...

To read a binary file, use the System.IO.BinaryReader class. Very simple.

MadHatter
October 30th, 2009, 03:21 PM
why would it's endianness be different than everything else?

DrBob
October 31st, 2009, 02:05 PM
I had to read raw database files that were produced on a Palm and the type of CPU uses by this system stores variables in the big-endian format.