首页 > 代码库 > Convert IPv6 Address to IP numbers (C#)
Convert IPv6 Address to IP numbers (C#)
URL: http://lite.ip2location.com/
Use the code below to convert the IP address of your web visitors and lookup for their geographical location, e.g. country, state, city, latitude/longitude, ZIPs, timezone and so on. Free database can be downloaded at http://lite.ip2location.com.
Expand | Embed | Plain Text
-
/// <summary>
-
/// Convert IPV6 Address to IP Number
-
/// Free geolocation database can be downloaded at:
-
/// http://lite.ip2location.com/
-
/// </summary>
-
-
string strIP = "2404:6800:4001:805::1006";
-
System.Net.IPAddress address;
-
System.Numerics.BigInteger ipnum;
-
-
if (System.Net.IPAddress.TryParse(strIP, out address)) {
-
byte[] addrBytes = address.GetAddressBytes();
-
-
if (System.BitConverter.IsLittleEndian) {
-
System.Collections.Generic.List<byte> byteList = new System.Collections.Generic.List<byte>(addrBytes);
-
byteList.Reverse();
-
addrBytes = byteList.ToArray();
-
}
-
-
if (addrBytes.Length > 8) {
-
//IPv6
-
ipnum = System.BitConverter.ToUInt64(addrBytes, 8);
-
ipnum <<= 64;
-
ipnum += System.BitConverter.ToUInt64(addrBytes, 0);
-
} else {
-
//IPv4
-
ipnum = System.BitConverter.ToUInt32(addrBytes, 0);
-
}
-
}
Convert IPv6 Address to IP numbers (C#)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。