C++ General: What do 'ntohl()' and 'htonl()' actually do?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1

Thread: C++ General: What do 'ntohl()' and 'htonl()' actually do?

Threaded View

  1. #1
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245

    C++ General: What do 'ntohl()' and 'htonl()' actually do?

    Q: What do 'ntohl()' and 'htonl()' actually do?

    A: 'ntohl()' and 'htonl()' are two of a family of four functions. The other two functions are 'ntohs()' and 'htons()'. Here are the descriptions of the functions from a Linux man page:

    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 network byte order to host byte order.
    This family of functions convert between host byte order and network byte order. When the two byte orders are different, this means the endian-ness of the data will be changed. When the two byte orders are the same, the data will not be changed. Because of this, these functions should not be used when one simply wants to change endian-ness in a platform independent (portable) way. This FAQ shows how to change endian-ness in a platform independent way.

    Special notes:
    • 'ntohs()', 'ntohl()', 'htons()', and 'htonl()' are not part of the C standard, and thus do not guarentee portability.
    • The POSIX implementations of 'ntohs()', 'ntohl()', 'htons()' and 'htonl()' take arguments of 'uint16_t' and 'uint32_t' argument types and can be found in the header file netinet/in.h.
    • The Windows implementations use 'unsigned short' and 'unsigned long' and can be found in the header file winsock2.h.
    • Other variants of 'ntoht()' and 'htont()' may exist on some sytems, such as 'ntohi()'/'htoni()' or 'ntohll()'/'htonll()'.



    Last edited by Andreas Masur; July 23rd, 2005 at 12:33 PM.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center