CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2005
    Posts
    25

    Question how to open file and read as little-endian or big-endian

    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.

  2. #2
    Join Date
    Oct 2009
    Posts
    8

    Re: how to open file and read as little-endian or big-endian

    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);
    }

  3. #3
    Join Date
    Oct 2009
    Posts
    8

    Re: how to open file and read as little-endian or big-endian

    I forgot...

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

  4. #4
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: how to open file and read as little-endian or big-endian

    why would it's endianness be different than everything else?

  5. #5
    Join Date
    Oct 2009
    Posts
    8

    Re: how to open file and read as little-endian or big-endian

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured