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) {
            printf("%s","host not found");
        }
     
        struct hostent *phe = gethostbyname(hostname);
        memcpy(&addr, phe->h_addr_list[0], sizeof(struct in_addr));
         
        //Convert IP to Decimal notation
        sprintf(decResult,"%u", addr);
        sprintf(decResult,"%u", htonl(atoi(decResult)));

But now is my question how do I reconvert it to the Dotted Decimal Notation?


I know it's done with 'inet_ntoa' function but I first need to get '3232235876' converted something else and then I need to convert that to addr.

To both those questions I don't know the answer :/

Kind regards.

Some more information I found:

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

The addresses returned by gethostbyname are arrays of bytes, not 64-bit integers. The conversion business is just about network byte order... all this means is the most significant byte (i.e., the first part of the IPv4 address) is the first byte in the sequence.

Consider this:

  • 3232235876
  • 0xC0A80164
  • (0xC0, 0xA8, 0x01, 0x64)
  • (192, 168, 1, 100)
  • 192.168.1.100
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.