Pejman Moghadam / C-programming

C - Convert quad-dotted-decimal notation into binary data in network byte order and vice versa

Public domain


-- 1 --

#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
        struct in_addr addr;
        char ip[]="192.168.29.57";

        if (inet_aton(ip, &addr) == NULL) {
                perror("inet_aton");
                exit(EXIT_FAILURE);
        }
        printf("%lu\n", addr.s_addr);
        exit(EXIT_SUCCESS);
}

-- 2 --

#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
        struct in_addr addr;
        char *ip;

        addr.s_addr=958245056;
        if ((ip=inet_ntoa(addr)) == NULL) {
                perror("inet_ntoa");
                exit(EXIT_FAILURE);
        }
        printf("%s\n", ip);
        exit(EXIT_SUCCESS);
}

BY: Pejman Moghadam
TAG: c, inet-aton, inet-ntoa
DATE: 2011-02-23 00:14:00


Pejman Moghadam / C-programming [ TXT ]