Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how to convert hext ip to decimal ip?

Posted on 2004-11-02
18
Medium Priority
?
2,146 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
Comment
Question by:cranium2003
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +4
18 Comments
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 12471282
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
ID: 12471303
>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
ID: 12471306
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:cranium2003
ID: 12472373
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
ID: 12472499
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
ID: 12472509
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:
stefan73 earned 200 total points
ID: 12472617
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 46

Expert Comment

by:Kent Olsen
ID: 12472751
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
ID: 12473091
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 46

Expert Comment

by:Kent Olsen
ID: 12473135

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
ID: 12473230
 :-D you can take over from now onwards.
its bed time for me.
0
 
LVL 23

Expert Comment

by:Mysidia
ID: 12479259
#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
ID: 12479360
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
ID: 12479764
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 46

Expert Comment

by:Kent Olsen
ID: 12482441
Hi Cranium2003,

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


Kent
0
 
LVL 12

Expert Comment

by:stefan73
ID: 12483077
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
ID: 12487242
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
ID: 12489270
Thanks all for helping me to solve my problem . i will see the ordering issue carefully as you people said.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

610 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