//======================================================== file = htonl.c =====
//=  A program to demonstate htonl and other similar sockets functions        =
//=============================================================================
//=  Notes:                                                                   =
//=    1) This program conditionally compiles for Winsock and BSD sockets.    =
//=       Set the initial #define to WIN or BSD as appropriate.               =
//=---------------------------------------------------------------------------=
//=  Example executions (on SPARC and Intel):                                 =
//=   Solaris on SPARC:                  Windows on Intel:                    =
//=     ptr[0] = 12                        ptr[0] = 78                        =
//=     ptr[1] = 34                        ptr[1] = 56                        =
//=     ptr[2] = 56                        ptr[2] = 34                        =
//=     ptr[3] = 78                        ptr[3] = 12                        =
//=     x = 12345678                       x = 12345678                       =
//=     ptr[0] = 12                        ptr[0] = 12                        =
//=     ptr[1] = 34                        ptr[1] = 34                        =
//=     ptr[2] = 56                        ptr[2] = 56                        =
//=     ptr[3] = 78                        ptr[3] = 78                        =
//=     y = 12345678                       y = 78563412                       =
//=     ptr[0] = 12                        ptr[0] = 78                        =
//=     ptr[1] = 34                        ptr[1] = 56                        =
//=     ptr[2] = 56                        ptr[2] = 34                        =
//=     ptr[3] = 78                        ptr[3] = 12                        =
//=     z = 12345678                       z = 12345678                       =
//=---------------------------------------------------------------------------=
//=  Build: bcc32 htonl.c or cl htonl.c wsock32.lib for Winsock               =
//=         gcc htonl.c -lnsl for BSD                                         =
//=---------------------------------------------------------------------------=
//=  Execute: htonl                                                           =
//=---------------------------------------------------------------------------=
//=  Author: Ken Christensen                                                  =
//=          University of South Florida                                      =
//=          WWW: http://www.csee.usf.edu/~christen                           =
//=          Email: christen@csee.usf.edu                                     =
//=---------------------------------------------------------------------------=
//=  History: KJC (09/16/03) - Genesis                                        =
//=           KJC (09/07/09) - Minor clean-up                                 =
//=============================================================================
#define  WIN                // WIN for Winsock and BSD for BSD sockets

//----- Include files ---------------------------------------------------------
#include <stdio.h>          // Needed for printf()
#include <stdlib.h>         // Needed for exit()
#include <string.h>         // Needed for memcpy() and strcpy()
#ifdef WIN
  #include <windows.h>      // Needed for all Winsock stuff
#endif
#ifdef BSD
  #include <sys/types.h>    // Needed for sockets stuff
  #include <netinet/in.h>   // Needed for sockets stuff
  #include <arpa/inet.h>    // Needed for sockets stuff
  #include <sys/socket.h>   // Needed for sockets stuff
#endif

//==============================================================================
//=  Main program                                                              =
//==============================================================================
int main(void)
{
  int             x, y, z;         // Integer values
  char           *ptr;             // Pointer

  // Assign x and output it in byte order and as a value
  x = 0x12345678;
  ptr = (char *) &x;
  printf("ptr[0] = %02X \n", ptr[0]);
  printf("ptr[1] = %02X \n", ptr[1]);
  printf("ptr[2] = %02X \n", ptr[2]);
  printf("ptr[3] = %02X \n", ptr[3]);
  printf("x = %08X \n", x);

  // Convert x to network order (big indian) and output it
  y = htonl(x);
  ptr = (char *) &y;
  printf("ptr[0] = %02X \n", ptr[0]);
  printf("ptr[1] = %02X \n", ptr[1]);
  printf("ptr[2] = %02X \n", ptr[2]);
  printf("ptr[3] = %02X \n", ptr[3]);
  printf("y = %08X \n", y);

  // Convert back to host order and output it
  z = ntohl(y);
  ptr = (char *) &z;
  printf("ptr[0] = %02X \n", ptr[0]);
  printf("ptr[1] = %02X \n", ptr[1]);
  printf("ptr[2] = %02X \n", ptr[2]);
  printf("ptr[3] = %02X \n", ptr[3]);
  printf("z = %08X \n", z);

  return(0);
}
