I'm trying to convert a string to IP address. The input string is an unsigned integer converted to std::string, for example "123456".
The code below is not correct, as it produces unreadable binary characters.
std::string str2IP(const std::string& address)
{
uint32_t ip = std::strtoul(address.c_str(), NULL, 0);
unsigned char bytes[4];
bytes[0] = ip & 0xFF;
bytes[1] = (ip >> 8) & 0xFF;
bytes[2] = (ip >> 16) & 0xFF;
bytes[3] = (ip >> 24) & 0xFF;
std::stringstream ss;
ss << bytes[3] << "." << bytes[2] << "." << bytes[1] << "." << bytes[0];
return ss.str();
}