Thread: Binary File - Byte order / endian

  1. #16
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    ok there is a function in:
    #include <winsock2.h>

    called:
    ntohs(DataPoint); <datapoint is a short... ntohs works for shorts

    but when i try to use it I get this

    Code:
    Linking...
    TestTile.obj : error LNK2001: unresolved external symbol __imp__ntohs@4
    Debug/TestTile.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.
    
    TestTile.exe - 2 error(s), 0 warning(s)
    here is the MSDN for it:
    http://msdn.microsoft.com/library/de...e_ordering.asp

    which means i am missing a library... does anyone know anything about this?

    OR

    does anyone know how that function i posted earlier does?

  2. #17
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    if anyone here has VS 2005.. can you look inside winsock2.h to see if there are any library includes.

  3. #18
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by chico1st View Post
    if anyone here has VS 2005.. can you look inside winsock2.h to see if there are any library includes.
    You need to link with wsock32.lib

  4. #19
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    wow you rock man.. thanks
    that worked perfectly

  5. #20
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    is there any way to check the endian-ness of a variable?

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by chico1st View Post
    is there any way to check the endian-ness of a variable?
    Yes. Something like this will work:
    Code:
    #define BIG_ENDIAN 1
    #define LITTLE_ENDIAN 2
    int endian_ness()
    {
       int x = 0x01000002;
       char *p = (char *)&x;
    
       if (x == 0x01) 
           return BIG_ENDIAN;
       else if (x == 0x02)
           return LITTLE_ENDIAN;
       else { 
           printf("Strange integer format or bad compiler\n");
           exit(1);
       }
    }
    --
    Mats

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It's worth noting that ALL variables on a particular system has the same endianness. [By the way, the word endian is related to the book "Gulliver's travels", where the two islands were at war because they wouldn't agree on which end of the egg one should start eating, the "thick" (big) end, or the "thin" (little) end - forming the warring factions of Big Endians and Little Endians - and of course, computer architects would discuss which is better at length!]

    --
    Mats

  8. #23
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> is there any way to check the endian-ness of a variable?

    endian-ness is simply the storage convention used by a CPU (within it's registers) to manipulate numbers. it has no effect on the numbers themselves.
    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;
    }

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

  10. #25
    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.

  11. #26
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Sebastiani View Post
    >> is there any way to check the endian-ness of a variable?

    endian-ness is simply the storage convention used by a CPU (within it's registers) to manipulate numbers. it has no effect on the numbers themselves.
    No, it's the EXTERNAL storage format in the memory that is defined by the endian-ness. The internal storage within the processor is completely invisible to an external observer - for all we know, the bits can be stored in any order in a register internally in the machine, as long as it "appears" that bit 31 is the most significant bit (and this doesn't matter if it's a BE or LE machine) and bit 0 is the least significant, and all bits in between come in the appropriate order. [That's for a 32-bit word - but it applies analogously to any size data that the processor will process as one unit].

    When the data is stored in memory, however, the data will be stored in a particular order: Big endian stores the most significant byte first, Little Endian stores the least significant byte first.

    --
    Mats

  12. #27
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by MacNilly View Post
    Don't Macs use Motorola processors and PC's use Intel processors? I never mentioned anything about OS's...
    IIRC, Macs used to use PowerPCs, and they relatively recently switched to Intels.

  13. #28
    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.

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