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.
// 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"
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;
This example shows how to convert from string to ip, and viceversa:
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);
The third inet_pton parameter is a pointer to an in_addr structure. After a successful inet_pton call, the in_addr structure will be populated with the address information. The structure's S_addr field contains the IP address in network byte order (reverse order).
Example :
#include <arpa/inet.h>
uint32_t NodeIpAddress::getIPv4AddressInteger(std::string IPv4Address) {
int result;
uint32_t IPv4Identifier = 0;
struct in_addr addr;
// store this IP address in sa:
result = inet_pton(AF_INET, IPv4Address.c_str(), &(addr));
if (result == -1) {
gpLogFile->Write(LOGPREFIX, LogFile::LOGLEVEL_ERROR, _T("Failed to convert IP %hs to IPv4 Address. Due to invalid family of %d. WSA Error of %d"), IPv4Address.c_str(), AF_INET, result);
}
else if (result == 0) {
gpLogFile->Write(LOGPREFIX, LogFile::LOGLEVEL_ERROR, _T("Failed to convert IP %hs to IPv4"), IPv4Address.c_str());
}
else {
IPv4Identifier = ntohl(*((uint32_t *)&(addr)));
}
return IPv4Identifier;
}
Best thing about these two functions is that they work for every address family
WSAStringToAddressA
This function takes to five arguments
•A pointer of array or array of char containing your address
•Address family of your address(Only used if argument 3 is NULL)
•WSAPROTOCOL_INFO structure providing information about your protocol, leave null if you want to use argument 2
•Pointer to the structure of sockaddr in which the result address has to be stored
•Pointer of the object of type DWORD in which the result length of your address has to be stored
WSAAddressToStringA
This function also takes five arguments
•Pointer to the structure of sockaddr that has to be translated
•Byte length of your address
•Structure of WSAPROTOCOL_INFO giving information about the address's protocol. Use NULL to make the function use address family of sockaddr structure in argument 1
•Array of char in which the address in the form of string has to be stored
•Length of the string form of your address in bytes