Formatting IPv6 as an int in C# and storing it in SQL Server

前端 未结 5 2208
无人及你
无人及你 2020-12-15 05:00

Under IPv4 I have been parsing the string representation of IP addresses to Int32 and storing them as INT in the SQL Server

5条回答
  •  轮回少年
    2020-12-15 05:45

    function encode_ip ($ip)
    {
        return bin2hex(inet_pton($ip));
    }
    
    function decode_ip($ip)
    {
        function hex2bin($temp) {
           $data="";
           for ($i=0; $i < strlen($temp); $i+=2) $data.=chr(hexdec(substr($temp,$i,2)));
           return $data;
        }
        return inet_ntop(hex2bin($ip));
    }
    

    -- max len row db
    echo strlen(inet_pton('2001:db8:85a3::8a2e:370:7334'));
    
    -- db row info
    ip varchar(16)
    
    -- sql binary save and read
    save base
    $bin_ip='0x'.bin2hex(inet_pton($data['ip_address']));
    
    -- db read
    select ip_address from users;
    
    -- encode binary from db
    echo inet_ntop($row['ip_address']);
    

提交回复
热议问题