How to convert string to IP address and vice versa
Solution 1
Use inet_ntop()
and inet_pton()
if you need it other way around. Do not use inet_ntoa(), inet_aton()
and similar as they are deprecated and don't support ipv6.
Here is a nice guide with quite a few examples.
// IPv4 demo of inet_ntop() and inet_pton()
struct sockaddr_in sa;
char str[INET_ADDRSTRLEN];
// store this IP address in sa:
inet_pton(AF_INET, "192.0.2.33", &(sa.sin_addr));
// now get it back and print it
inet_ntop(AF_INET, &(sa.sin_addr), str, INET_ADDRSTRLEN);
printf("%s\n", str); // prints "192.0.2.33"
Solution 2
I'm not sure if I understood the question properly.
Anyway, are you looking for this:
std::string ip ="192.168.1.54";
std::stringstream s(ip);
int a,b,c,d; //to store the 4 ints
char ch; //to temporarily store the '.'
s >> a >> ch >> b >> ch >> c >> ch >> d;
std::cout << a << " " << b << " " << c << " "<< d;
192 168 1 54
Solution 3
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.
Solution 4
inet_ntoa()
converts a in_addr
to string:
The inet_ntoa function converts an (Ipv4) Internet network address into an ASCII string in Internet standard dotted-decimal format.
inet_addr()
does the reverse job
The inet_addr function converts a string containing an IPv4 dotted-decimal address into a proper address for the IN_ADDR structure
PS this the first result googling "in_addr to string"!
Solution 5
To convert string to in-addr:
in_addr maskAddr;
inet_aton(netMaskStr, &maskAddr);
To convert in_addr to string:
char saddr[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &inaddr, saddr, INET_ADDRSTRLEN);
Safari
Updated on July 30, 2020Comments
-
Safari almost 4 years
how can I convert a string ipAddress (struct in_addr) and vice versa? and how do I turn in unsigned long ipAddress? thanks
-
Milan about 13 yearsThose functions are deprecated and should not be used.
-
CharlesB about 13 years@Milan OK. Though the OP uses in_addr struct that deal with ipv4
-
Safari about 13 yearsWhat should I do to use them under Windows? What should I include?
-
Safari about 13 yearsWhat should I do to use them under Windows? What should I include?
-
Milan about 13 yearsIf using winsock then InetNtop and InetPton on windows vista and later. Header file Ws2tcpip.h . A tip is to look at examples on msdn.
-
CharlesB about 13 yearsThe answer is in the function's documentation, (follow the link): include Winsock2.h.
-
Milan about 13 yearsWSAAddressToString (ntop) and WSAStringToAddress (pton) in Winsock2.h. I would suggest an upgrade to at least vista, and to ask you that next time you specify what platform you are developing when asking questions :)
-
Carcigenicate almost 9 years@Milan Thanks! Everywhere says to use the inet prefix functions, but only 1 of them is included in Winsock2.
-
Remy Lebeau over 6 yearsThere is also
RtlIpv(4|6)AddressToString/Ex()
andRtlIpv(4|6)StringToAddress/Ex()
functions -
martian almost 6 yearsThis link is not available any more
-
Qi Fan about 5 yearsWhat do "p" and "n" mean in these functions? If I know the meanings I can probably remember them better.
-
JustWe about 5 yearsWhat about IPv6?
-
Marked as Duplicate almost 5 years@QiFan The "n" stands for "network", and the "p" for "(text) presentation".
-
Luk over 4 years@Milan: Hey, do you know if there is a function to achieve exactly the same, but that allows for a name as src-string instead of the IP address in dotted decimal form? I want to give a name that can be resolved by my DNS server instead
-
Alexis over 3 yearsadding the header would have been nice too
-
Cerin over 3 yearsThis doesn't work.
localhost
resolves to arbitrary IPs that have nothing to do with my actual IP. -
mercury almost 3 yearsI am wondering why all languages consider ip as a string than a 32 bit integer?