Solved

how to convert hext ip to decimal ip?

Posted on 2004-11-02
2,059 Views
Last Modified: 2012-06-21
Is there any function available in C on linux that can convert  HEX IP address to Decimal IP address? if not then how to write it?
0
Question by:cranium2003
    18 Comments
     
    LVL 16

    Expert Comment

    by:PaulCaswell
    I'm not clear what a HEX IP address would be but I'd guess it's 4 binary bytes. How about:

    unsigned char hexIP [4] = {0x12,0x34,0x56,0x78};

    printf("%d.%d.%d.%d", hexIP[0], hexIP[1], hexIP[2], hexIP[3] );

    Paul
    0
     
    LVL 45

    Expert Comment

    by:sunnycoder
    >Is there any function available in C on linux that can convert  HEX IP address to Decimal IP address? if not then how to write it
    Hex and decimal are just 2 ways of interpreting the same data ... A conversion is not required here. Perhaps all you need is a change of format specifier to printf

    Also, your question is a bit strange. We noramlly convert IP address from 32 bit unsigned int to decimal and dot notation and vice versa. It is rarely expressed in hex. To be honest, I never used hex or for that matter saw hex being used for IP address. Are you sure you are working with IP address and not mac address? What exactly are you trying to do.
    0
     
    LVL 5

    Expert Comment

    by:van_dy
    how is your ipaddress stored?

    is it stored in a string like:
    char *ip = "ab:3c:12:2f";  //though i havent seen this notation

    or it is stored as a hex number like :
    int ip = 0xee388d8;


    0
     

    Author Comment

    by:cranium2003
    hi van_dy,
                 whatever the Hex ip stored in file is having formats in form of 6600000A. i want to read it as string and make its interpretation as 10.0.0.102. how can i do that?
    0
     
    LVL 5

    Expert Comment

    by:van_dy
    you dont need to read the ip as a string
    you can read it into an unsigned integer.
    decimal, hexadecimal notations for integers
    represent the same data.(no special handling
    required for a hexadecimal integer)
    consider:

    struct in_addr ip;
    char *dod_ip = NULL;  //dotted decimal ip
    FILE *f = fopen(..);
    fseek(...);  .... //use fseek to go to the character position where your ip is stored;
    fscanf(f, "%x", &ip.s_addr);    //assuming 32 bit ip addresses

    once you have the ip in an integer there are various functions
    you can use to convert it to dotted decimal notation. The easiest
    one would be inet_ntoa();

    ip.s_addr = htonl(ip.s_addr);      // be sure to store the ip in network byte order
    dod_ip = inet_ntoa(ip);

    there are other functions you might condider using, like inet_ntop. Also look
    into their manpages for more information.

    hope this helps,
    van_dy
    0
     
    LVL 12

    Expert Comment

    by:stefan73
    Hi sunnycoder,
    > To be honest, I never used hex or for that matter saw hex being used
    > for IP address
    Tuxedo's WSNADDR is an example.

    Cheers!

    Stefan
    0
     
    LVL 12

    Accepted Solution

    by:
    cranium2003,

    Your IP is in host order. You can try something like

    #include <sys/types.h>
    #include <netinet/in.h>
    #include <inttypes.h>
    #include <stdio.h>

    int main(){
        char* hexaddr = "6600000A";
       
        uint32_t host_ip;
        uint32_t net_ip;
       
        sscanf(hexaddr,"%08x",&host_ip);
       
        printf("Host format: %08x\n",host_ip);
       
        net_ip=htonl(host_ip);
        printf("Net  format: %08x\n",net_ip);
       
        printf("%d.%d.%d.%d\n",net_ip>>24,(net_ip>>16) & 0xff,  (net_ip>>8) & 0xff, net_ip & 0xff);

        return 0;
    }
    0
     
    LVL 45

    Expert Comment

    by:Kdo
    Hi cranium2003,

    >whatever the Hex ip stored in file is having formats in form of 6600000A. i want to read it as string and make its interpretation as 10.0.0.102. how can i do that?


    The IP protocol states that the octets (bytes) in an IP address are consecutive, left to right.  As such, storing the address 10.0.0.102 as 0x6600000A on a little-endian machine violates this rule.  You might have a reason for your program to want to do this, but it's useless to the IP protocol.

    Most Intel and AMD machines are little endian.  This means that the bytes in an integer are stored in memory backwards.  The value 0x6600000A will "reverse" to become 0x0A000066 when read from memory and placed into a register.  0x0A000066 is the TRUE value that you're dealing with here.  Break this into four bytes and the four values are 0x0A 0x00 0x00 0x66.  Convert these to decimal and the four bytes contain 10 0 0 102 -- exactly your IP address.

    If you want to convert a 32-bit integer to an IP address, it's easy.  Just grab the 4-bytes of the integer and print them as unsigned integers.  Disregard possible byte swapping.  The hardware will handle that, if necessary, when the data is read from memory.

    unsigned int IPInt = 0x0a000066;

      printf ("%d.%d.%d.%d", (IPInt >> 24) & 0xFF, (IPInt >> 16) & 0xFF, (IPInt >> 8) & 0xFF, IPInt & 0xFF);


    If you want to convert a string to an IP address, you must understand that the string representation should be left-to-right.  Your example suggests that the string is right-to-left.  You can certainly convert this, but I don't know what good it will be to you.

    Assuming that the string is left-to-right, you have several easy options.  The string in the example is in hex, so if you want to display the octets in hex, simply display the string two characters at a time and put a period between the values.

    Printing the decimal values of the octets is even easier.  Using the example above:

    char *IpString = "0x0A000066";
    unsigned int IPInt;

      IPInt = atoi (IpString);  /* You can also use atol()  */
      printf ("%d.%d.%d.%d", (IPInt >> 24) & 0xFF, (IPInt >> 16) & 0xFF, (IPInt >> 8) & 0xFF, IPInt & 0xFF);



    Good Luck,
    Kent
    0
     
    LVL 5

    Expert Comment

    by:van_dy
    Oh well, thanks to Kent.

    ip.s_addr = htonl(ip.s_addr);         // this line in my last post isnt required if the values are stored in your file as u specified.
    .
    Please remove it.
    0
     
    LVL 45

    Expert Comment

    by:Kdo

    Hi Van_dy,

    Man....  I hate having my code obsoleted before lunch.  I mean, I'm still in "coffee" mode!

    ;)

    0
     
    LVL 5

    Expert Comment

    by:van_dy
     :-D you can take over from now onwards.
    its bed time for me.
    0
     
    LVL 23

    Expert Comment

    by:Mysidia
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>

    ...

    int hexval;
    struct in_addr addy;
    char* result;

    hexval = strtoul ( ip_address_hex_string_goes_here, (char **)NULL, 16 );
    addy.s_addr = hexval;
    result = inet_ntoa(&addy);

    printf("Result is: %s", result);


    Just be cautious, as in general, inet_ntoa returns a pointer to static data
    0
     

    Author Comment

    by:cranium2003
    hello all,
              Thanks for the respone to my question.
    Stefan73,
                  You got it exactly what i want and your solution work for me. thanks. But one more think i want
    > printf("%d.%d.%d.%d\n",net_ip>>24,(net_ip>>16) & 0xff,  (net_ip>>8) & 0xff, net_ip & 0xff);
    above statement prints what i want in decimal IP address now i want it to be treated as single string. how can i do that?
    after retrieving ip address in decimal string form i am using it for communicating that ip through sockets.
    0
     
    LVL 23

    Expert Comment

    by:Mysidia
    create the buffer and sprintf into it...

    i.e.

    char buf[256];

    sprintf(buf, "%d.%d.%d.%d",  net_ip >> 24,  (net_ip >> 16)& 255, (net_ip>>8) & 255, net_ip & 255);
    0
     
    LVL 45

    Expert Comment

    by:Kdo
    Hi Cranium2003,

    Careful here ....  The IP string that you've provided is BACKWARDS!!!!


    Kent
    0
     
    LVL 12

    Expert Comment

    by:stefan73
    Yup, that's why I called htonl() in my example.

    Cranium: When you have something in host order (i.e., Intel), ALWAYS use htonl().
    Having a string in the wrong order (that's NOT host order!) is particularly bad, since the order is equally wrong on both little and big endian machines. Additionally, network order is big-endian, to htonl() doesn't do anything there...
    0
     
    LVL 23

    Expert Comment

    by:Mysidia
    Yes, issuing htonl first is quite important if your "hex ip" is not represented in the standard
    format for use by network protocols.

    (Which might be the case if you got your "hex address" by using raw data from the address structure
     after calling inet_pton or inet_aton)


    Big/Little/PDP Endian orderings are the most common, but there are many possible byte
    order conventions that various hosts could use for their representations of integers.
    0
     

    Author Comment

    by:cranium2003
    Thanks all for helping me to solve my problem . i will see the ordering issue carefully as you people said.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now