IPv6 parsing in C

前端 未结 7 1120
猫巷女王i
猫巷女王i 2020-12-15 08:35

I wanted to know how I can parse an IPv6 address in C and convert it to a 128 bit value?

So a hex address like 1:22:333:aaaa:b:c:d:e needs to be convert

7条回答
  •  粉色の甜心
    2020-12-15 08:58

    if you can use boost, something like this should work:

    #include
    
    using boost::asio::ip;
    
    bool parseIpv6String(std::string ipv6_string, char* dest){
        try{
            address_v6 addr = address_v6::from_string(ipv6_string);
            memcpy(dest,addr.to_bytes().data(), 16);
        }catch(...){
            return false;
        }
        return true;
    }
    

    It is a bit more portable than for example POSIX specific functions.

提交回复
热议问题