Find the answer to your Linux question:
Results 1 to 9 of 9
I have a small doubt regarding endianess. My system here is little endian and the board I am communicating with is big endian. Hence the data received by the board ...
  1. #1
    Just Joined!
    Join Date
    Jun 2009
    Posts
    16

    endianess problem!!!!

    I have a small doubt regarding endianess. My system here is little endian and the board I am communicating with is big endian. Hence the data received by the board is not same as what I am sending. I have heard about htons and htonl,are they used only for IP address conversion or even for data? I just need to make sure that data should be matched both the sides,so can anybody please help me out!!!!Thanks in advance.......

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    The network-byte-order conversion functions are useful for any data that is in network byte order. You can use them on one end to convert the data to network-byte-order and on the other end to convert to the local representation. If you send the data directly, without conversion, then you will have to deal with the conversions yourself, just to be safe.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Jun 2009
    Posts
    16

    Thanks....

    That means i need to convert the data into big-endian before transmitting to board and then back to board's byte-order right?

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    If network-byte-order ( nbo ) is big-endian, then yes. It's been so long since I had to deal with that cruft that I forgot what order it is. The technique I mentioned is how you transmit data from one system to another when you don't know what the endian-ness of the receiving machine happens to be. Then sender encodes and the receiver decodes. If they are compatible with nbo, then that is a do-nothing operation with no performance hit since typically the htons/htonl calls are compiler macros. Of course, if you are sending data structures, you also have to accomodate structure padding issues which is why most protocols that deal with binary data have some means to marshal structural data in a way that can be unambiguously decoded at the receiving end.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  5. #5
    Just Joined!
    Join Date
    Jun 2009
    Posts
    16
    Thanks a lot.Will let you know if at all I face any kind of problem!!!!!

  6. #6
    Just Joined!
    Join Date
    Jun 2009
    Posts
    4
    Hi San,
    Did u face any kind of problem while implementing the above detail.I couldnt make out the exact procedural emphasis of the network-byte-order posted by rubberman.I have a query related to the same issue.My board is also facing the same problem.In case of latent information how will the encoder-decoder react?Even iam spending a lot of time on that.Please elucidate ur understanding on the whole scenario.
    Thanks

  7. #7
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Quote Originally Posted by nonemmaverick View Post
    Hi San,
    Did u face any kind of problem while implementing the above detail.I couldnt make out the exact procedural emphasis of the network-byte-order posted by rubberman.I have a query related to the same issue.My board is also facing the same problem.In case of latent information how will the encoder-decoder react?Even iam spending a lot of time on that.Please elucidate ur understanding on the whole scenario.
    Thanks
    Whenever you send binary data between systems on a network, on the sending side you need to encode each component item into network byte order (NBO). On the receiving end, you need to decode from NBO to native representation. Doing so will assure you that the data is correct after accounting for endian-ness. IE, the integer 1234 on the sending side will still be 1234 on the receiving side. There are macros to help you do this. From the htonl man page:
    Code:
    BYTEORDER(3)               Linux Programmer’s Manual              BYTEORDER(3)
    
    NAME
           htonl,  htons,  ntohl,  ntohs  - convert values between host and network byte
           order
    
    SYNOPSIS
           #include <arpa/inet.h>
    
           uint32_t htonl(uint32_t hostlong);
    
           uint16_t htons(uint16_t hostshort);
    
           uint32_t ntohl(uint32_t netlong);
    
           uint16_t ntohs(uint16_t netshort);
    
    DESCRIPTION
           The htonl() function converts the unsigned integer hostlong  from  host  byte
           order to network byte order.
    
           The  htons() function converts the unsigned short integer hostshort from host
           byte order to network byte order.
    
           The ntohl() function converts the unsigned integer netlong from network  byte
           order to host byte order.
    
           The  ntohs()  function converts the unsigned short integer netshort from net-
           work byte order to host byte order.
    
           On the i80x86 the host byte order is Least Significant  Byte  first,  whereas
           the  network  byte  order,  as used on the Internet, is Most Significant Byte
           first.
    
    CONFORMING TO
           POSIX.1-2001.
    
           Some  systems  require   the   inclusion   of   <netinet/in.h>   instead   of
           <arpa/inet.h>.
    
    SEE ALSO
           gethostbyname(3), getservent(3)
    
    BSD                               1993-04-15                      BYTEORDER(3)
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  8. #8
    Just Joined!
    Join Date
    Jun 2009
    Posts
    4
    hi rubberman,
    thanks a lot.that really helped.

  9. #9
    Just Joined!
    Join Date
    Jun 2009
    Posts
    16
    was about to reply to your Q,but i hope what rubberman xplained will do rite????

Posting Permissions

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