• C

how to convert hext ip to decimal ip?

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?
cranium2003Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PaulCaswellCommented:
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
sunnycoderCommented:
>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
van_dyCommented:
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
Prepare for an Exciting Career in Cybersecurity

Help prevent cyber-threats and provide solutions to safeguard our global digital economy. Earn your MS in Cybersecurity. WGU’s MSCSIA degree program curriculum features two internationally recognized certifications from the EC-Council at no additional time or cost.

cranium2003Author Commented:
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
van_dyCommented:
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
stefan73Commented:
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
stefan73Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kent OlsenData Warehouse Architect / DBACommented:
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
van_dyCommented:
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
Kent OlsenData Warehouse Architect / DBACommented:

Hi Van_dy,

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

;)

0
van_dyCommented:
 :-D you can take over from now onwards.
its bed time for me.
0
MysidiaCommented:
#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
cranium2003Author Commented:
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
MysidiaCommented:
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
Kent OlsenData Warehouse Architect / DBACommented:
Hi Cranium2003,

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


Kent
0
stefan73Commented:
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
MysidiaCommented:
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
cranium2003Author Commented:
Thanks all for helping me to solve my problem . i will see the ordering issue carefully as you people said.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.