Sponsored Content
Full Discussion: ntohs
Top Forums Programming ntohs Post 6940 by rwb1959 on Sunday 16th of September 2001 11:17:49 AM
Old 09-16-2001
Are you trying to write your own htons/ntohs
functions? If you use these functions when
transferring data across a network between
machines, you don't have to worry about it
since the compiled program will do it correctly
for that hardware architecture.

The rule is:
Network byte order, as used on the
Internet, is Most Significant Byte first.

On the i80x86 the host byte order is Least Significant Byte first.
 

We Also Found This Discussion For You

1. UNIX for Dummies Questions & Answers

htons and ntohs

Hi All, I am running a short program on the effects of host-to-network and network-to-host functions. The following is part of my code: int a = 384; printf("Used htons with a, getting: %x Hex and %d Numeral\n", htons(a), htons(a)); printf("Used ntohs with a, getting: %x Hex and %d... (1 Reply)
Discussion started by: dbshuis
1 Replies
HTON(3) 						     Library Functions Manual							   HTON(3)

NAME
hton, htons, htonl, ntohs, ntohl - host to network byte order conversion SYNOPSIS
#define _MINIX_SOURCE 1 #include <stddef.h> #include <sys/types.h> #include <net/hton.h> u16_t htons(u16_t host_word) u32_t htonl(u32_t host_dword) u16_t ntohs(u16_t network_word) u32_t ntohl(u32_t network_dword) u16_t HTONS(u16_t host_word) u32_t HTONL(u32_t host_dword) u16_t NTOHS(u16_t network_word) u32_t NTOHL(u32_t network_dword) DESCRIPTION
These macros convert 16-bit and 32-bit quantities to and from the network byte order used by the TCP/IP protocols. The function of the macros is encoded in their name. H means host byte order, n means network byte order, s means a 16-bit quantity and l means a 32-bit quan- tity. Thus htons converts a 16-bit quantity from host byte order to network byte order. The difference between the lower case and upper case variants is that the lower case variants evaluate the argument at most once and the upper case variants can be used for constant fold- ing. That is, htonl(f(x)) will call f(x) at most once and HTONS(0x10) will be equivalent to 0x10 on a big-endian machine and 0x1000 on a little-endian machine. SEE ALSO
ip(4). AUTHOR
Philip Homburg (philip@cs.vu.nl) HTON(3)
All times are GMT -4. The time now is 12:00 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy