How to convert string to IP address and vice versa

how can i convert ipAddress string (struct in_addr) and vice versa? and how do I enable unsigned long ipAddress? thank

+58
c ++ string struct ip-address
Mar 16 2018-11-11T00:
source share
8 answers

Use inet_ntop() and inet_pton() if you need it the other way around. Do not use inet_ntoa(), inet_aton() and the like, as they are deprecated and do not support ipv6.

Here is a good guide with 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" 
+108
Mar 16 '11 at 16:11
source share

I am not sure if I understood the question correctly.

Anyway, you are 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; 

Exit:

 192 168 1 54 
+5
Mar 16 '11 at 16:12
source share

I managed to convert the string to DWORD and back using 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 on a project for servicing old MFC code, so converting legacy function calls is not applicable.

+4
Jun 26 '14 at 9:13
source share

inet_ntoa() converts the in_addr string to a string:

The inet_ntoa function converts (IPv4) the Internet address to an ASCII string in the dotted decimal format in the Internet standard.

inet_addr() does the reverse job

The inet_addr function converts a string containing an IPv4 dotted decimal address to an appropriate address for the IN_ADDR structure

PS this is the first result of googling "in_addr to string"!

+3
Mar 16 2018-11-11T00:
source share

To convert a string to in-addr:

 in_addr maskAddr; inet_aton(netMaskStr, &maskAddr); 

To convert in_addr to a string:

 char saddr[INET_ADDRSTRLEN]; inet_ntop(AF_INET, &inaddr, saddr, INET_ADDRSTRLEN); 
+2
Mar 16 '11 at 16:25
source share

This example shows how to convert from string to ip and vice versa:

 struct sockaddr_in sa; char ip_saver[INET_ADDRSTRLEN]; // store this IP address in sa: inet_pton(AF_INET, "192.0.1.10", &(sa.sin_addr)); // now get it back sprintf(ip_saver, "%s", sa.sin_addr)); // prints "192.0.2.10" printf("%s\n", ip_saver); 
0
Apr 15 '17 at 20:23
source share

The third parameter to inet_pton is a pointer to the in_addr structure. After a successful inet_pton call, the in_addr structure will be populated with address information. The structure of the S_addr field contains the IP address in network byte order (reverse order).

0
Aug 07 '18 at 9:27
source share

here are the easy-to-use, thread-oriented c ++ functions for converting uint32_t native-endian to string and string to native-endian uint32_t:

 #include <arpa/inet.h> // inet_ntop & inet_pton #include <string.h> // strerror_r #include <arpa/inet.h> // ntohl & htonl using namespace std; // im lazy string ipv4_int_to_string(uint32_t in, bool *const success = nullptr) { string ret(INET_ADDRSTRLEN, '\0'); in = htonl(in); const bool _success = (NULL != inet_ntop(AF_INET, &in, &ret[0], ret.size())); if (success) { *success = _success; } if (_success) { ret.pop_back(); // remove null-terminator required by inet_ntop } else if (!success) { char buf[200] = {0}; strerror_r(errno, buf, sizeof(buf)); throw std::runtime_error(string("error converting ipv4 int to string ") + to_string(errno) + string(": ") + string(buf)); } return ret; } // return is native-endian // when an error occurs: if success ptr is given, it set to false, otherwise a std::runtime_error is thrown. uint32_t ipv4_string_to_int(const string &in, bool *const success = nullptr) { uint32_t ret; const bool _success = (1 == inet_pton(AF_INET, in.c_str(), &ret)); ret = ntohl(ret); if (success) { *success = _success; } else if (!_success) { char buf[200] = {0}; strerror_r(errno, buf, sizeof(buf)); throw std::runtime_error(string("error converting ipv4 string to int ") + to_string(errno) + string(": ") + string(buf)); } return ret; } 

honest warning, at the time of writing, they are not verified. but these functions are exactly what I was looking for when I came to this thread.

0
Jan 22 '19 at 18:39
source share



All Articles