天天看點

将十六進制參數轉換成點分十進制 -- 将點分十進制參數轉換成十六進制

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

int my_htonl(char *argv)

{

    struct in_addr inaddr;//網絡位元組序

    unsigned int addr;//點分十進制

    sscanf(argv, "%x", &addr);

    inaddr.s_addr = htonl(addr);

    printf("%s/n", inet_ntoa(inaddr));

    return 0;

}

int my_ntohl(char * argv)

{

    struct in_addr inaddr;//網絡位元組序

    unsigned int addr;//點分十進制

    if(inet_aton(argv, &inaddr) != 0){

        addr = ntohl(inaddr.s_addr);

        printf("0x%x/n", addr);

    }

    return 0;

}

int main( )

{

    char * test_arry1  = "0x8002c2f2";

    char * test_arry2  = "128.2.194.242";

    my_htonl(test_arry1 );

    my_ntohl(test_arry2);

    return 0;

}