Question

Is there any function in the Boost library, in which I can convert and IPv4 address given as string:

std::string ip_address = "192.168.0.1";

Into a binary format as following "11000000101010000000000000000001" and store it in a boost::unit32 unit, or I should build the function by myself?

Also what about converting an IPv6 address from std::string into 4 * unit32, is there any function to convert it into octets?

Was it helpful?

Solution

Boost.Asio will get you there, as in:

boost::asio::ip::address_v4::from_string("192.168.0.1").to_ulong();

IPv6 is a little more complicated, as you have to use boost::asio::ip::address_v6::from_string(addr).to_bytes() which returns a boost::array<unsigned char, 16> and convert from that yourself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top