Thread: Binary File - Byte order / endian

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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
    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.

  9. #9
    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.

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

  11. #11
    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

  12. #12
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Quote Originally Posted by KIBO View Post
    little/big endian is processor related, not OS
    Don't Macs use Motorola processors and PC's use Intel processors? I never mentioned anything about OS's...

  13. #13
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Quote Originally Posted by brewbuck View Post
    Yet more thigh-slapping hilarity.
    What's so funny?

    If I made a binary file on a PC writing the words 10, 20, 30, and 40, and you tried to read that on a Mac, you're telling me you would get 10, 20, 30 and 40?
    Last edited by MacNilly; 08-22-2007 at 05:47 PM.

  14. #14
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MacNilly View Post
    If I made a binary file on a PC writing the words 10, 20, 30, and 40, and you tried to read that on a Mac, you're telling me you would get 10, 20, 30 and 40?
    Given that my Mac has an Intel chip, I would have to say... yes.

    Properly written software takes endianness into account. Just because a file is "binary" doesn't mean it can't be manipulated on processors of differing endianness.

  15. #15
    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?

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