LINUX TIPS AND TRICKS --- April 06, 2001

Published by ITworld.com -- changing the way you view IT
http://www.itworld.com/newsletters
______________________________________________________________________

Byte Ordering
By Danny Kalev

This week, I will discuss a familiar cross-platform data transfer 
issue, namely byte ordering.

Big Endian and Little Endian
TCP/IP networks are rarely homogenous; they usually consist of a wide 
variety of machines, architectures, and operating systems ? each 
storing binary data in various forms. The actual storage format is 
transparent as long as you access data locally (i.e., on the same 
machine). However, you must consider the data storage format 
differences when transmitting or receiving data across machines and 
networks.

Any data larger than a single byte can be store its bytes at least two 
ways: big endian order and little endian order. Big endian machines 
store the most significant byte at the lowest memory address. The 
remaining bytes occupy higher memory addresses, in ascending order. For 
example, the 32-bit hexadecimal integer 0x0a0b0c0d has four bytes with 
the following values: 0a, 0b, 0c, 0d. The first, and most significant, 
byte, 0a, occupies the lowest memory address. The second byte, 0b, 
occupies a memory address higher than the first byte's address, and the 
rest follow in kind. By contrast, little endian architectures use 
exactly the opposite byte ordering. Thus, under a little endian 
architecture, the most significant byte, 0a, would occupy the highest 
memory address.

Network Byte Order
The TCP/IP protocol mandates big endian byte ordering for transmitting 
multibyte values. For this reason, a big endian byte order is also 
called a "network byte order". If the target host uses a little endian 
order, then it must convert all incoming multibyte values from network 
order to little endian order. Likewise, before transmitting multibyte 
values via TCP/IP, such a host must convert them to network byte order. 
Fortunately, you don't have to reinvent the wheel by writing the 
conversion code. The <netinet/in.h> header file declares four functions 
that convert between host byte order and network byte order:

    unsigned int   htonl (unsigned int hostlong);
    unsigned short htons (unsigned short hostshort);
    unsigned int   ntohl (unsigned int netlong);
    unsigned short ntohs (unsigned short netshort);

Note that no signed counterparts for these functions exist. Although 
they take and return unsigned values, they handle signed values just as 
well.

Suppose your machine uses little endian order. To transmit the 32-bit 
value 0x0a0b0c0d over a TCP/IP connection you have to call htonl() and 
transmit the result:

    transmit_num(htonl(0x0a0b0c0d));

Likewise, to convert an incoming 32-bit value, use ntohl():

    int n=ntohl(get_number_from_network());

About the author(s)
-------------------
Danny Kalev is a system analyst and software engineer with more than 10 
years of experience, specializing in C++ and object-oriented analysis 
and design on various platforms including VMS, DOS, Windows, Unix, and 
Linux. His technical interests involve code optimization, networking, 
and distributed computing. He is also a member of the ANSI C++ 
standardization committee and the author of ANSI/ISO C++ Professional 
Programmer's Handbook (Que, 1999). Contact him at linuxnl@excite.com.
______________________________________________________________________

ADDITIONAL RESOURCES

The Gnutella file-sharing network and Java 
Use the JTella API to easily develop applications that 
access Gnutella

http://www.javaworld.com/jjw/lintps_nl/jw-10-2000/
	jw-1006-fileshare.html

Under the Hood: Bytecode basics 
A first look at the bytecodes of the Java virtual machine

http://www.javaworld.com/jjw/lintps_nl/jw-09-1996/
	jw-09-bytecodes.html

Mounting Network File Systems the easy way

http://www.itworld.com/jump/lintps_nl/www.itworld.com/Net/
	3400/LWD001012geek1/

The structure of a SAN 
A look at the components that put terabytes at your fingertips

http://www.itworld.com/jump/lintps_nl/www.itworld.com/Comp/
	3805/ITW2259/
_______________________________________________________________

COMMUNITY DISCUSSIONS

Unix-NT Integration
Unix and NT systems integrators join forces to ease the pain 
when the odd couple gets cozy.

http://www.itworld.com/jump/lintps_nl/forums.itworld.com/
	webx?14@@.ee6b841/103!skip=58

Linux Software Development
Hone your Linux development skills, share your expertise, and 
put out the occasional call for help in this discussion for 
programmers of all levels.

http://www.itworld.com/jump/lintps_nl/forums.itworld.com/
	webx?14@@.ee6b652/304!skip=253
_______________________________________________________________

CONTACTS

* For editorial comments, write Andrew Santosusso, Associate 
  Editor, Newsletters at:
  andrew_santosusso@itworld.com

* For advertising information, write Dan Chupka, Account 
  Executive at:
  dan_chupka@itworld.com

* For recruitment advertising information, write Jamie Swartz, 
  Eastern Regional Sales Manager at: jamie_swartz@itworld.com 
  or Paul Duthie, Western Regional Sales Manager at:
  paul_duthie@itworld.com

* For all other inquiries, write Jodie Naze, Product Manager, 
  Newsletters at:
  jodie_naze@itworld.com
_______________________________________________________________

PRIVACY POLICY

http://www.itworld.com/Privacy/

Copyright 2001 ITworld.com, Inc., All Rights Reserved.
http://www.itworld.com
