문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top