Thread: little to big endian

  1. #1
    Registered User
    Join Date
    Mar 2013

    little to big endian


    I am trying to read in a file that is written in little Endian and convert it to big Endian. How do I do this?


  2. #2
    Registered User
    Join Date
    Jun 2005
    Your question is meaningless since you have provided insufficient information.

    If your file is a straight text (non-binary) no conversion is needed.

    If your file is binary, you need an unambiguous specification of how the data was written. Without such a specification, no conversion is possible.

    In particular, the specification will cover whether data in the (binary) file was written as 16-bit, 32-bit, or 64-bit integral types - each of those has different needs for converting endianess. If your file contains a mix of 16-bit, 32-bit, and 64-bit integral data, you need to know unambiguously what the mix is. For example, a conversion will not work if you assume a given block of eight bytes was written as a 64-bit integer, but it was actually written as a pair of 32-bit integers.

    If your binary file contains floating point data (written as float, double, or long double) then no conversion is possible.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    May 2012
    Quote Originally Posted by mgravier View Post

    I am trying to read in a file that is written in little Endian and convert it to big Endian. How do I do this?

    You write functions readint16be, readint32be, readint64be. Then you use the shift and logical operators to build up the number. So a 16 bit integer would be (ch1 << 8) | ch2. To be fully portable you need to sign extend, but generally it's good enough to use the type of the right size.
    The code then works regardless of whether your machine is big or little endian.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fiddling with the bits(Big Endian vs Little Endian)
    By A34Chris in forum C Programming
    Replies: 71
    Last Post: 11-26-2012, 10:39 PM
  2. Little endian and Bit endian advantages and disadvantages
    By nkrao123@gmail. in forum C Programming
    Replies: 4
    Last Post: 09-11-2011, 04:40 AM
  3. big endian-small endian problem
    By kapil1089thekin in forum C Programming
    Replies: 3
    Last Post: 05-15-2008, 06:47 PM
  4. Big Endian Little Endian Complex- Converting Characters
    By bd02eagle in forum C Programming
    Replies: 3
    Last Post: 07-11-2006, 01:01 AM
  5. Big endian/Little endian conversion
    By bonkey in forum Windows Programming
    Replies: 5
    Last Post: 09-25-2002, 02:29 PM