How to convert string to IP address and vice versa

前端 未结 9 759
傲寒
傲寒 2020-11-28 19:43

how can I convert a string ipAddress (struct in_addr) and vice versa? and how do I turn in unsigned long ipAddress? thanks

9条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 20:42

    I was able to convert string to DWORD and back with this code:

    char strAddr[] = "127.0.0.1"
    DWORD ip = inet_addr(strAddr); // ip contains 16777343 [0x0100007f in hex]
    
    struct in_addr paddr;
    paddr.S_un.S_addr = ip;
    
    char *strAdd2 = inet_ntoa(paddr); // strAdd2 contains the same string as strAdd
    

    I am working in a maintenance project of old MFC code, so converting deprecated functions calls is not applicable.

提交回复
热议问题