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

前端 未结 5 2207
无人及你
无人及你 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:55

    Just as an IPv4 address is really a 32 bit number, an IPv6 address is really a 128 bit number. There are different string representations of the addresses, but the actual address is the number, not the string.

    So, you don't convert an IP address to a number, you parse a string representation of the address into the actual address.

    Not even a decimal can hold a 128 bit number, so that leaves three obvious alternatives:

    • store the numeric value split into two bigint fields
    • store a string representation of the address in a varchar field
    • store the numeric value in a 16 byte binary field

    Neither is as convenient as storing an IPv4 address in an int, so you have to consider their limitations against what you need to do with the addresses.

提交回复
热议问题