フォーマットでのIPv6をintとしてクライアントまで、フルのC#とで格納するSQLサーバー
-
13-09-2019 - |
質問
下 IPv4
して構文解析の文字列表現のIPアドレス Int32
および保管として INT
の SQL Server
.
今、 IPv6
ようにしている場合が標準または受け入れ方を解析しの文字列表現 IPv6
二 Int64
を使用 C#
?
またどのように人の保存価値の SQL Server
として二つの分野 BIGINT
?
解決
ことを目的として、IPv4アドレスは32ビット数を、IPv6アドレスは128ビット番号です。が異なる文字列表現は、アドレスが、実際の住所を番号ではなく、文字列になります。
かなに変換するIPアドレスには、構文の文字列表現住所の実アドレスです。
なものを decimal
できる128ビット数、その葉をつか選択肢:
- 店舗の数値分
bigint
分野 - 店内の文字列表現住所に
varchar
分野 - 店内の数値は、16バイト
binary
分野
に関する情報は見つかりませんで利用することのないようとして保存IPv4アドレスに int
, ですから、その制限に対し必要なものなのです。
他のヒント
最も簡単なルートの枠組みをいいます。使用 IPAddress.Parse
構文解析のアドレス、その IPAddress.GetAddressBytes
の"番号"としてbyte[].
最後に、分ける配列の次の8バイトに変換する二Int64sなどにより作成 MemoryStream
以上のバイト配列として読書を BinaryReader
.
これを回避する必要が理解し利用可能な全てのショートカット表現のためのIPv6アドレス.
ご利用の場合はSQL Server2005を使用できます uniqueidentifier
タイプです。この店舗は16バイトのブランドイメージを満足させ、IPv6ipアドレスです。を変換でき間 IPAddress
や Guid
のコンストラクタおよび ToByteArray
.
私は、以下の方法に変換するIPアドレスにつ UInt64
s(C#3.0).
/// <summary>
/// Converts an IP address to its UInt64[2] equivalent.
/// For an IPv4 address, the first element will be 0,
/// and the second will be a UInt32 representation of the four bytes.
/// For an IPv6 address, the first element will be a UInt64
/// representation of the first eight bytes, and the second will be the
/// last eight bytes.
/// </summary>
/// <param name="ipAddress">The IP address to convert.</param>
/// <returns></returns>
private static ulong[] ConvertIPAddressToUInt64Array(string ipAddress)
{
byte[] addrBytes = System.Net.IPAddress.Parse(ipAddress).GetAddressBytes();
if (System.BitConverter.IsLittleEndian)
{
//little-endian machines store multi-byte integers with the
//least significant byte first. this is a problem, as integer
//values are sent over the network in big-endian mode. reversing
//the order of the bytes is a quick way to get the BitConverter
//methods to convert the byte arrays in big-endian mode.
System.Collections.Generic.List<byte> byteList = new System.Collections.Generic.List<byte>(addrBytes);
byteList.Reverse();
addrBytes = byteList.ToArray();
}
ulong[] addrWords = new ulong[2];
if (addrBytes.Length > 8)
{
addrWords[0] = System.BitConverter.ToUInt64(addrBytes, 8);
addrWords[1] = System.BitConverter.ToUInt64(addrBytes, 0);
}
else
{
addrWords[0] = 0;
addrWords[1] = System.BitConverter.ToUInt32(addrBytes, 0);
}
return addrWords;
}
ただくにはキャストにお UInt64
s Int64
s前のデータベース、また ArgumentException
.かの値しなければならないとき、それを鋳型で固めてに戻る UInt64
の符号なし値とします。
持っていないの逆(に変換する UInt64[2]
IP string)いもので築方法です。
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']);