Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

C - convert IP address (dotted decimal) to long and back

Posted on 2007-07-24
9
Medium Priority
?
17,040 Views
Last Modified: 2009-12-26
Hello, how do I convert from IP address to network byte order and then back to IP address using char and long?

I have:

char ipaddress[15]='127.0.0.1';
long x;
x=inet_addr(ipaddress);
printf("x=%d\n",x);

but how do I do the reverse if I know x equals some value and want to convert back to a character array with the dotted decimal IP?

Thanks!
0
Comment
Question by:jchristn123
  • 4
  • 4
9 Comments
 
LVL 58

Expert Comment

by:amit_g
ID: 19561262
char* ipaddress_r = inet_ntoa(x);
0
 
LVL 1

Author Comment

by:jchristn123
ID: 19561288
Hi,

Tried that, when I compiled I got

error: incompatible type for argument 1 of 'inet_ntoa'

Any thoughts?  Here is the remainder of the code:

int main(int nArg, char* pszArgs[])
{
  char ipaddr[15]="127.0.0.1";
  long x;
  char ipaddrx[15];
  x=inet_addr(ipaddr);
  printf("inet_addr(%s)=%l\n",ipaddr,x);
  char* ipaddry=inet_ntoa(x);
  printf("inet_ntoa(x)=%s\n",*ipaddry);
  return 0;
}


Thanks!
0
 
LVL 58

Expert Comment

by:amit_g
ID: 19561403
The parameter should be of type struct in_addr...

struct in_addr x_r;

x_r.S_un.S_addr = x;

also x should be unsigned long.
0
Technology Partners: 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!

 
LVL 1

Author Comment

by:jchristn123
ID: 19561419
Hi Amit G

Still getting errors.  Here is where I am:

{
  char ipaddr[15]="127.0.0.1";
  unsigned long x;
  char ipaddrx[15];
  x=inet_addr(ipaddr);
  printf("inet_addr(%s)=%l\n",ipaddr,x);
  struct in_addr x_r;
  x_r.S_un.S_addr=x;
  char* ipaddry=inet_ntoa(x);
  printf("inet_ntoa(x)=%s\n",*ipaddry);
  return 0;
}

And the error:

htonl.c:26: error: 'struct in_addr' has no member named 'S_un'
htonl.c:27: error: incompatible type for argument 1 of 'inet_ntoa'

Also tried with lower-case 's' on x_r.s_un.s_addr and same error.

Any other thoughts?

Thanks
0
 
LVL 58

Expert Comment

by:amit_g
ID: 19561457
inet_ntoa(x) should be inet_ntoa(x_r)

also what include files are you using and what platform? Windows?
0
 
LVL 1

Author Comment

by:jchristn123
ID: 19561487
Hi Amit G

htonl.c:26: error: 'struct in_addr' has no member named 'S_un'

Changing x to x_r took care of the first one.

I'm using a Mac, include files are below (this is just one piece of a much larger program).

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <netdb.h>

Thanks!
0
 
LVL 58

Accepted Solution

by:
amit_g earned 1200 total points
ID: 19561496
Use

x_r.s_addr = x;
0
 
LVL 1

Author Comment

by:jchristn123
ID: 19561526
Getting warmer :)  Now it compiles but I get a bus error:
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <netdb.h>

int main(int nArg, char* pszArgs[])
{
  char ipaddr[15]="127.0.0.1";
  unsigned long x;
  char ipaddrx[15];
  x=inet_addr(ipaddr);
  printf("inet_addr(%s)=%d\n",ipaddr,x);
  struct in_addr x_r;
  x_r.s_addr=x;
  char* ipaddry=inet_ntoa(x_r);
  printf("inet_ntoa(x_r)=%s\n",*ipaddry);
  return 0;
}


inet_addr(127.0.0.1)=16777343
Bus error
(ran it on a FC6 box and it produced a segmentation fault)
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 800 total points
ID: 19561635
Simple pointer dereferencing error:

  printf("inet_ntoa(x_r)=%s\n",*ipaddry);

should read

  printf("inet_ntoa(x_r)=%s\n",ipaddry);

without the '*'.
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 opening and reading files in the C programming language.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

810 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