Like Tree1Likes
  • 1 Post By anduril462

Convert Decimal IP To Dotted Decimal Notation

This is a discussion on Convert Decimal IP To Dotted Decimal Notation within the C Programming forums, part of the General Programming Boards category; Hallo! I got the following Decimal IP: "3232235876" it represents "192.168.1.100" I got it in the following way: Code: //GET ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    45

    Exclamation 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) {
            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?

    Kind regards.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,469
    Look into the inet_ntoa function.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    45
    Quote Originally Posted by anduril462 View Post
    Look into the inet_ntoa function.
    Thank you! I seemed to be using the other one

    Rep for you

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    45
    Quote Originally Posted by anduril462 View Post
    Look into the inet_ntoa function.
    One problem how do I convert a char[] to in_addr?

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    45
    Quote Originally Posted by anduril462 View Post
    Look into the inet_ntoa function.
    Because I can't convert 3232235876 straight to 192.168.1.100?

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,469
    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)
    {
        char dotted_quad[16];
        struct addrinfo *addr, *rp;
    
    
        if (getaddrinfo("google.com", NULL, NULL, &addr) == -1) {
            perror("getaddrinfo");
            return EXIT_FAILURE;
        }
    
    
        for (rp = addr; rp != NULL; rp = rp->ai_next) {
            if (inet_ntop(AF_INET, &(((struct sockaddr_in *) rp->ai_addr)->sin_addr), dotted_quad, sizeof(dotted_quad)) == NULL) {
                perror("inet_ntop");
            }
            else {
                puts(dotted_quad);
            }
        }
    
    
        return EXIT_SUCCESS;
    }

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    45
    Quote Originally Posted by anduril462 View Post
    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)
    {
        char dotted_quad[16];
        struct addrinfo *addr, *rp;
    
    
        if (getaddrinfo("google.com", NULL, NULL, &addr) == -1) {
            perror("getaddrinfo");
            return EXIT_FAILURE;
        }
    
    
        for (rp = addr; rp != NULL; rp = rp->ai_next) {
            if (inet_ntop(AF_INET, &(((struct sockaddr_in *) rp->ai_addr)->sin_addr), dotted_quad, sizeof(dotted_quad)) == NULL) {
                perror("inet_ntop");
            }
            else {
                puts(dotted_quad);
            }
        }
    
    
        return EXIT_SUCCESS;
    }
    With the code is nothing wrong I get the right result

    I need to get the following things:

    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

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,469
    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. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    45
    Quote Originally Posted by anduril462 View Post
    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;
        struct addrinfo hints, *servinfo;
        struct in_addr addr;
        socklen_t addr_size;
    
        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) {
            printf("%s","host not found");
        }
    
    --> 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)
        memcpy(&addr, phe->h_addr_list[0], sizeof(struct in_addr));
         
        //Convert IP to Decimal notation
    
    -->I convert my IP to 1677830336 wich represents: 100.1.168.192
         sprintf(decResult,"%u", addr);
    
    -->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. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,469
    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;
    char *dotted_quad;
    ...
    memcpy(&numeric_ip, phe->h_addr_list[0], sizeof(numeric_ip));  // copy into unsigned long
    numeric_ip = htonl(numeric_ip);  // flip the byte order
    memcpy(&addr, &numeric_ip, sizeof(numeric_ip));
    dotted_quad = inet_ntoa(addr);
    MaSSaSLaYeR likes this.

  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    45
    Quote Originally Posted by anduril462 View Post
    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;
    char *dotted_quad;
    ...
    Memcpy(&numeric_ip, phe->h_addr_list[0], sizeof(numeric_ip));  // copy into unsigned long
    numeric_ip = htonl(numeric_ip);  // flip the byte order
    memcpy(&addr, &numeric_ip, sizeof(numeric_ip));
    dotted_quad = inet_ntoa(addr);
    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. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,469
    Haha. You're welcome x 90.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Decimal convert
    By uscuba2 in forum C Programming
    Replies: 4
    Last Post: 04-22-2009, 01:58 AM
  2. Convert.ToDecimal vs (decimal)
    By Pete_O in forum C# Programming
    Replies: 5
    Last Post: 09-20-2007, 08:36 AM
  3. how to convert decimal to hexa decimal in C/
    By kalamram in forum C Programming
    Replies: 4
    Last Post: 09-03-2007, 07:39 AM
  4. convert a decimal int to hex
    By pinkcheese in forum C++ Programming
    Replies: 12
    Last Post: 08-12-2002, 09:15 PM
  5. decimal to binary, decimal to hexadecimal and vice versa
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2001, 10:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21