Solved

convert a network address into a dottted string.

Posted on 1998-12-18
4
220 Views
Last Modified: 2008-03-03
I want to know how to convert an (int)IP into a standard dotted format string.

I know about inet_ntoa, but I want to do know how to it directly???????

thanks
 
0
Comment
Question by:cosmicmike
[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
  • 2
4 Comments
 
LVL 3

Expert Comment

by:marcjb
ID: 1255293
Start by converting the number to a string.  This can be done with the sprintf function.

#include<stdio.h>

int main()
{
int num = 8989;
char the_num[15];
sprintf(the_num, "%i", num);
return 0;
}

The code above puts the number 8989 into the string the_num.

You could then manipulate this string however you like, including using a function like sprintf again to place '.' where you need.

0
 
LVL 32

Expert Comment

by:jhance
ID: 1255294
marcjb,

This won't always work right as it fails to take into account the machine's byte ordering scheme.

cosmic,

I fail to see your reluctance to do it the right way:

#include "stdio.h"
#include "winsock.h"

int main(int argc, char* argv[])
{
      struct in_addr in;
      in.S_un.S_addr = 0x12345678;


      printf("%s\n", inet_ntoa(in));
      return 0;
}
0
 

Author Comment

by:cosmicmike
ID: 1255295
sorry marcjb your solution is not 100%

jhance
ok I have an array of int IP nums and I want to convert them in a for next loop and put the dotted results in a char array !!

0
 
LVL 32

Accepted Solution

by:
jhance earned 50 total points
ID: 1255296
OK, how about this?


#include "stdio.h"
#include "winsock.h"

int main(int argc, char* argv[])
{
      char cIP[16][16];
      int iIP[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
      int i;

      struct in_addr in;

      for(i=0; i<16; i++){
            in.S_un.S_addr = iIP[i];
            strcpy(cIP[i], inet_ntoa(in));
      }


      for(i=0; i<16; i++){
            printf("0x%x = %s\n", iIP[i], cIP[i]);
      }

      return 0;
}
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

632 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