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
  1. /// <summary>
  2. /// Convert IPV6 Address to IP Number
  3. /// Free geolocation database can be downloaded at:
  4. /// http://lite.ip2location.com/
  5. /// </summary>
  6.  
  7. string strIP = "2404:6800:4001:805::1006";
  8. System.Net.IPAddress address;
  9. System.Numerics.BigInteger ipnum;
  10.  
  11. if (System.Net.IPAddress.TryParse(strIP, out address)) {
  12. byte[] addrBytes = address.GetAddressBytes();
  13.  
  14. if (System.BitConverter.IsLittleEndian) {
  15. System.Collections.Generic.List<byte> byteList = new System.Collections.Generic.List<byte>(addrBytes);
  16. byteList. Reverse();
  17. addrBytes = byteList.ToArray();
  18. }
  19.  
  20. if (addrBytes.Length > 8) {
  21. //IPv6
  22. ipnum = System.BitConverter.ToUInt64(addrBytes, 8);
  23. ipnum <<= 64;
  24. ipnum += System.BitConverter.ToUInt64(addrBytes, 0);
  25. } else {
  26. //IPv4
  27. ipnum = System.BitConverter.ToUInt32(addrBytes, 0);
  28. }
  29. }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值