How to convert IP to hex using C++ ?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: How to convert IP to hex using C++ ?

  1. #1
    Join Date
    Jul 2012
    Posts
    25

    Question How to convert IP to hex using C++ ?

    Hi guy .
    I don`t known how to convert IP to hex .
    In this link http://www.mustat.com/70.42.23.121 (Ip host codeguru.com) . I see hex IP : Hex IP 0x462a1779
    I want to convert IP "70.42.23.121" to "0x462a1779"


    Code:
    #include <winsock.h>
    #include <windows.h>
    #include <stdio.h>
    
    UINT32 ip_to_hex(UINT32 ip_add)
    {
       UINT32 ip_hex;
       //code convert ip to hex
       return ip_hex; 
    }
    
    void main()
    {
      //ip_to_hex(70.42.23.121);
      system("pause");
    }
    Did you try IP "123.30.128.10" , IP hex : 0x7b1e800a
    When i try "%x" , it`s wrong

    Code:
            printf("%x \n", 123);
    	printf("%x \n", 30);
    	printf("%x \n", 128);
    	printf("%x \n", 10);

    And this is result
    7b 1e 80 and a , not 0a
    further i think we must separate each octect by a (dot) . , Then convert decimal to hexa
    Can you help me ?
    Thank guy.

  2. #2
    Join Date
    Sep 2012
    Posts
    1

    Re: How to convert IP to hex using C++ ?

    Do this:
    Code:
    printf("0%x \n", 10);
    But if it's greater than decimal 16 ( hex 10) then the 0%x is not needed.

  3. #3
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,634

    Re: How to convert IP to hex using C++ ?

    Last edited by S_M_A; September 27th, 2012 at 12:20 PM.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: How to convert IP to hex using C++ ?

    I don't understand your ip_to_hex() function. If you want to convert a string with the ip in this format "70.42.23.121" then your function should take a string and not an integer.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Jul 2012
    Posts
    25

    Re: How to convert IP to hex using C++ ?

    Hi S_M_A , I have tried that code , but it`s wrong . Did you try http://www.mustat.com/127.0.0.1 . you can see ip hex.

    Hi Clark_Kent_SuperCodr and cilu
    Can you demo some of code for me ?

    Code:
    UINT32  ip_to_hex(char ip_add)
    {
    	UINT32 ip_hex;
    	ip_hex =inet_addr(( ip_add).ToChar);
    	//printf("0x%08x\n", ip_hex);
    	return ip_hex;
    }
    Thank for you.
    Last edited by headshot; September 27th, 2012 at 09:11 PM.

  6. #6
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: How to convert IP to hex using C++ ?

    headshot, you don't seem to understand the difference between integers, characters or strings. I think you need to start with the basics and then get to more complicated things.

    Code:
    unsigned int ip_to_hex(const char* strip)
    {
       unsigned int ip = 0;
       char* str = _strdup(strip);
       char delimiters[] = ".";
       char* token = strtok(str, delimiters);
       int tokencount = 0;
       while(token != NULL)
       {
          if(tokencount < 4)
          {
             int n = atoi(token);
             ip = ip | n << 8 * (3 - tokencount);
          }
          tokencount++;
          token = strtok(NULL, delimiters);
       }
       free(str);
    
       if(tokencount != 4)
          ip = 0;
    
       return ip;
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
       int ip = ip_to_hex("127.0.0.1");
    
       return 0;
    }
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  7. #7
    Join Date
    Jul 2012
    Posts
    25

    Re: How to convert IP to hex using C++ ?

    Hi cilu .
    Yes, my ideas exactly what you do
    Thank you so much

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center