1 out of 8 rated this helpful - Rate this topic

ntohl function

Applies to: desktop apps only

The ntohl function converts a u_long from TCP/IP network order to host byte order (which is little-endian on Intel processors).

Syntax

u_long WSAAPI ntohl(
  __in  u_long netlong
);

Parameters

netlong [in]

A 32-bit number in TCP/IP network byte order.

Return value

The ntohl function returns the value supplied in the netlong parameter with the byte order reversed. If netlong is already in host byte order, then this function will reverse it. It is up to the application to determine if the byte order must be reversed.

Remarks

The ntohl function takes a 32-bit number in TCP/IP network byte order (the AF_INET or AF_INET6 address family) and returns a 32-bit number in host byte order.

The ntohl function can be used to convert an IPv4 address in network byte order to the IPv4 address in host byte order. This function does not do any checking to determine if the netlong parameter is a valid IPv4 address.

The ntohl function does not require that the Winsock DLL has previously been loaded with a successful call to the WSAStartup function.

Requirements

Minimum supported client

Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Header

Winsock2.h

Library

Ws2_32.lib

DLL

Ws2_32.dll

See also

Winsock Reference
Winsock Functions
htonl
htons
inet_addr
inet_ntoa
InetNtop
ntohs
WSAHtonl
WSAHtons
WSANtohl
WSANtohs

 

 

Send comments about this topic to Microsoft

Build date: 4/24/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Application doesn't worry - API defines this for correct endianness.

"It is up to the application to determine if the byte order must be reversed."

It is up to the operating system providing this routine to determine if it actually does anything or not - not the application to detect if it should be used.  Network byte order is big endian - on those machines this routine does nothing.  On little endian machines, this function swaps the byte order.

If you're sending an integer over a network, you should call

valuetosend = hton*( value )
to encode it and
 value = ntoh*( receivedvalue )
to decode it.  Even if you're sending between two same endian machines, this will ensure that your code remains portable.