# Thread: Convert Decimal IP To Dotted Decimal Notation

1. ## Convert Decimal IP To Dotted Decimal Notation

Hallo!

I got the following Decimal IP: "3232235876" it represents "192.168.1.100"

I got it in the following way:

Code:
```
//GET IP
if (gethostname(hostname, sizeof(hostname)) == SOCKET_ERROR) {
}

struct hostent *phe = gethostbyname(hostname);

//Convert IP to Decimal notation
sprintf(decResult,"%u", htonl(atoi(decResult)));```
But now is my question how do I reconvert it to the Dotted Decimal Notation?

Kind regards.

2. Look into the inet_ntoa function.

3. Originally Posted by anduril462
Look into the inet_ntoa function.
Thank you! I seemed to be using the other one

Rep for you

4. Originally Posted by anduril462
Look into the inet_ntoa function.
One problem how do I convert a char[] to in_addr?

5. Originally Posted by anduril462
Look into the inet_ntoa function.
Because I can't convert 3232235876 straight to 192.168.1.100?

6. Looking through the man pages for some of these functions, it seems gethostbyname and inet_ntoa are deprecated. Instead you should use getaddrinfo and inet_ntop. Here's an example:
Code:
```#include <stdio.h>
#include <stdlib.h>

#define _POSIX_SOURCE
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>

int main(void)
{

return EXIT_FAILURE;
}

for (rp = addr; rp != NULL; rp = rp->ai_next) {
perror("inet_ntop");
}
else {
}
}

return EXIT_SUCCESS;
}```

7. Originally Posted by anduril462
Looking through the man pages for some of these functions, it seems gethostbyname and inet_ntoa are deprecated. Instead you should use getaddrinfo and inet_ntop. Here's an example:
Code:
```#include <stdio.h>
#include <stdlib.h>

#define _POSIX_SOURCE
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>

int main(void)
{

return EXIT_FAILURE;
}

for (rp = addr; rp != NULL; rp = rp->ai_next) {
perror("inet_ntop");
}
else {
}
}

return EXIT_SUCCESS;
}```
With the code is nothing wrong I get the right result

I need to get the following things:

If you see in my code example there is a value "addr" it has the value "1677830336".

To get the correct solution I first need to convert "3232235876" to "1677830336" and then use ntohl on "1677830336" to get the correct result

8. You didn't show very much code. You left out critical definitions of variables so I don't know their type, among other things. Where does addr come from? How does it get the value 1677830336? What does that value represent? What is the process of converting 1677830336 to 3232235876 and back? Is it just changing the endianness? You are not being very clear.

9. Originally Posted by anduril462
You didn't show very much code. You left out critical definitions of variables so I don't know their type, among other things. Where does addr come from? How does it get the value 1677830336? What does that value represent? What is the process of converting 1677830336 to 3232235876 and back? Is it just changing the endianness? You are not being very clear.
First off I'm very sorry for my unclearness.

Variables:
Code:
```    struct sockaddr_storage their_addr;

int len, bytes_sent, bytes_received, fdmax, new_fd, nbytes, j, i;
int  countSentBytes=0,sockfd ,bindfd , listenfd;

//char *sendToUser = (char*)inputUser, *fileName = (char*)inputFileName, *fileLoc = (char*)inputFileLoc;
char strSizeFile[50], buffer[1024], hostip[30], read[500], sendDCC[500], decResult[50], hostname[80], *pch;
char buf[500], remoteIP[INET6_ADDRSTRLEN], MYPORT[6] = "49153";```

Code:
```--> I get my hostname from my current host.
if (gethostname(hostname, sizeof(hostname)) == SOCKET_ERROR) {
}

--> I give the value from hostname to phe
struct hostent *phe = gethostbyname(hostname);

-->I get the ip address from my current hostname (the name from ipconfig)

//Convert IP to Decimal notation

-->I convert my IP to 1677830336 wich represents: 100.1.168.192

-->I flip it around and get as result: 3232235876 the decimal notation of the IP address
sprintf(decResult,"\n%u", htonl(atoi(decResult)));```
The reason I don't give the value straight away to htonl is because of this thing:

Code:
```'ntohl' : cannot convert parameter 1 from 'in_addr' to 'u_long'

'No suitable conversion exists'```

I'm working with DCC now wich is a part of the IRC Protocol. When sending files DCC requires the IP to be converted to the decimal notation before sending a packet out.

On receiving a message to create a connection with DCC I need to convert this decimal notation back to Dotted Decimal Notation. So the only thing I get when setting up a new Connection is the "3232235876".

I hope i cleared things up a lil bit.

If you have any further questions, feel free to ask.

10. Ahh, I see now. You need to copy the contents of phe->h_addr_list[0] into an unsigned long, then call htonl on it, then memcpy that into addr. Finally, you can pass addr to inet_ntoa. Note that uses the deprecated functions, but that's ultimately your call. This is untested, but should be close:
Code:
```// existing declarations remain, just add this
unsigned long numeric_ip;
...
memcpy(&numeric_ip, phe->h_addr_list[0], sizeof(numeric_ip));  // copy into unsigned long
numeric_ip = htonl(numeric_ip);  // flip the byte order

11. Originally Posted by anduril462
ahh, i see now. You need to copy the contents of phe->h_addr_list[0] into an unsigned long, then call htonl on it, then memcpy that into addr. Finally, you can pass addr to inet_ntoa. Note that uses the deprecated functions, but that's ultimately your call. This is untested, but should be close:
Code:
```// existing declarations remain, just add this
unsigned long numeric_ip;
...
Memcpy(&numeric_ip, phe->h_addr_list[0], sizeof(numeric_ip));  // copy into unsigned long
numeric_ip = htonl(numeric_ip);  // flip the byte order
yes yes finally thank god i am almost in tears almost you nailed it man

thank you thank you thank you thank you thank you thank you thank you thank you thank you
thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you
thank you thank you thank you thank you thank you thank you thank you thank you thank you
thank you thank you thank you thank you thank you thank you thank you thank you thank you
thank you thank you thank you thank you thank you thank you thank you thank you thank you
thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you
thank you thank you thank you thank you thank you thank you thank you thank you thank you
thank you thank you thank you thank you thank you thank you thank you thank you thank you

12. Haha. You're welcome x 90.