convert a network address into a dottted string.

Posted on 1998-12-18
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???????

Question by:cosmicmike
  • 2

Expert Comment

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


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.

LVL 32

Expert Comment

ID: 1255294

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


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;

Author Comment

ID: 1255295
sorry marcjb your solution is not 100%

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 !!

LVL 32

Accepted Solution

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;

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to create project in Eclipse ? 1 193
How to organize data in excel ? 2 112
Adjust Mfcapp 29 160
C++ :Change value from  DisableCMD registry 4 47
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and reading files 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.

914 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

18 Experts available now in Live!

Get 1:1 Help Now