Thread: Binary File - Byte order / endian

  1. #1
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168

    Binary File - Byte order / endian

    I have to write a binary file.
    The data is supposed to be written in Little Endian format, which is a type of byte order.
    Does anyone know how to do this?

    Glossary:

    byte order:
    byte order sets the endian form of the resulting data. Byte order, or endian form, indicates whether integers are represented in memory from most-significant byte to least-significant byte or vice versa.
    little-endian—The least-significant byte occupies the lowest memory address. Used on Windows and Linux.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, I know how to do that. [That is the question you asked! :-)]

    Do you have any thoughts on the matter?

    --
    Mats

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, I'll be kind and give a hint: Try finding out what byte-order your machine is...

    --
    Mats

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your reference says
    little-endian—The least-significant byte occupies the lowest memory address. Used on Windows and Linux.
    The part that says "Used in Windows and Linux" is true - just like "dogs have a tail" - doesn't mean that all animals with a tail are dogs, right?

    I don't know about Windows, but Linux can and does definitely run on big-endian machines, no doubt at all about that.

    --
    Mats

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    first you need to determine the byte ordering of the machine your program is running under.
    a simple way to determine this would be:

    Code:
    int
    is_big_endian_machine(void)
    {
        static
        unsigned
        tst = 1;
    
        static
        int
        stat = (*(unsigned char*)&tst) != 1;
    
        return stat;
    }
    if it's big endian, simply reverse the bytes of each 'native' data type.
    the easiest way to do that is to cast the address of the data to an unsigned char* and then work with the data as an array of bytes.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Isn't it also possible to do it portably without caring about a particular implementation's underlying multibyte storage? That is, where you truncate to a byte some integral value to get the least significant byte to put to a file, and then right-shift the value by CHAR_BIT; doing so for each byte in the object.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Quote Originally Posted by chico1st View Post
    I have to write a binary file.
    The only problem I can see is if you wanted to move a file written from one type of machine to the other. Guess that's why you can't move files from Macs to PCs.

    You want to try and convert a file from one to the other? Unless you know the specific data types that make up the file, that would be impossible.

  8. #8
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    so to switch the byte order my code would look like this:

    Code:
    buffer1 = (unsigned char *)OldNumber;
    bit[0] = find the first bit;
    bit[1] = find the second bit
    etc...
    bit[8] = ...
    
    sprintf(Buffer2, "&#37;c%c%c%c%c%c%c%c", bit[0], bit[1],...);
    NewNumber = (unsigned short)Buffer2;
    or something to that extent?

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MacNilly View Post
    The only problem I can see is if you wanted to move a file written from one type of machine to the other. Guess that's why you can't move files from Macs to PCs.
    Yet more thigh-slapping hilarity.

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by brewbuck View Post
    Yet more thigh-slapping hilarity.
    Yeah, I kind of wondered about that.

  11. #11
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    little/big endian is processor related, not OS

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by KIBO View Post
    little/big endian is processor related, not OS
    At least to a part, hence my comment:
    The part that says "Used in Windows and Linux" is true - just like "dogs have a tail" - doesn't mean that all animals with a tail are dogs, right?

    I don't know about Windows, but Linux can and does definitely run on big-endian machines, no doubt at all about that.
    But at the same time, there are several processors I know of (Mips, 29K) that can actually set the byte-ordering by changing a bit in a control-register. I think ARM can do this too. In this case, the OS can decide what byte-ordering to use.

    --
    Mats

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> or something to that extent?

    consider the 4 byte hexidecimal number 0xbebafeca. on a big endian machine this would be stored in contigiuos memory as [be][ba][fe][ca], whereas on a little endian machine the format would be [ca][fe][ba][be]. viewed as an array of unsigned char, to reverse the byte order you would need simply to swap the first element of the array with the last and the second with the third.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  14. #14
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    oh, phew i thought i had to switch every single bit.

    so if i had 'unsigned shorts' (the data type), would i literally take the number lets say 16, which is 00000000 00010000 in binary, little endian format would be 00010000 00000000, which in integer is 4096. Then write that number to the binary file i have to write?

  15. #15
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    ok i googled the topic and i found a very simple code to swap these endians .. but it is in C++ therefore i dont know whats going on.

    Can you help me to convert this to C?

    Code:
    unsigned short ByteSwap4 (unsigned short nValue)
    {
       return (((nValue>> 8)) | (nValue << 8));
    
    }
    its from here.. and there is a little story to go with the artical
    http://www.codeproject.com/cpp/endianness.asp
    Last edited by chico1st; 08-22-2007 at 10:00 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. copying binary file
    By samc2004 in forum C Programming
    Replies: 5
    Last Post: 12-09-2003, 01:34 PM