#include <arpa/inet.h>
#include <stdio.h>
/*
* Just musing around, some useful little functions in the pocket...
*/
int main(int argc,char *argv[]) {
unsigned char *p;
// What ordering my computer uses ? Mine (i7) is little-endian, and yours ?
int i = 0x01020304;
p = (char *)&i;
printf("%02x%02x%02x%02x\n",p[0],p[1],p[2],p[3]);
// First convert dotted Internet address into NBO 32 bits struct
struct in_addr addr;
printf("inet_aton %d\n",inet_aton("172.23.128.25",&addr));
// What is this number in HO ?
printf("Network byte orderring %08x\n",addr.s_addr);
// Exhibit bytes with NBO..
p = (char *)&(addr.s_addr);
printf("%02x.%02x.%02x.%02x\n",p[0],p[1],p[2],p[3]);
// Ok, now try simpler? conversion
in_addr_t addr2;
addr2 = inet_addr("172.23.128.25");
printf("Network byte ordering %08x\n",addr2);
// Uh? Why would you like an HO address ???
in_addr_t addr3;
addr3 = inet_network("172.23.128.25");
printf("Host byte ordering : %08x\n",addr3);
// Woh! invert from NBO to string (char *)...
char rep[INET_ADDRSTRLEN];
inet_ntop(AF_INET,&addr,rep,INET_ADDRSTRLEN);
printf("%s\n",rep);
return 0;
}