Convert IPv6 Address to IP numbers (C#)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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. }

 

以上是关于Convert IPv6 Address to IP numbers (C#)的主要内容,如果未能解决你的问题,请参考以下文章

Python 语言实现 IPv6 是否ping通

ipv4与ipv6 Inet4Address类和Inet6Address类

Codeforces 758D Ability To Convert(区间DP)

IPv6 Can't assign requested address

IPV6详解:Stable Privacy Address

IPV6详解:Stable Privacy Address Kernel实现分析