博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Convert IPv6 Address to IP numbers (C#)
阅读量:7062 次
发布时间:2019-06-28

本文共 1093 字,大约阅读时间需要 3 分钟。

URL: 

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 |  | 
  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 = 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. }

 

转载于:https://www.cnblogs.com/waw/p/6003380.html

你可能感兴趣的文章
购物车--low版
查看>>
linux
查看>>
PHP中的替换strtr
查看>>
Apache和nginx 301重定向
查看>>
LINQ分页和排序,skip和Take 用法
查看>>
Activiti 查找流程状态(流程下一步)
查看>>
Delphi 密码限3次登录程序(附:源码)
查看>>
Linux中大量TIME_WAIT的解决办法
查看>>
Angular UI Route
查看>>
一个应届毕业生程序员的独白
查看>>
oracle的全局临时表
查看>>
python用sql的limit语句进行分页
查看>>
编译安装ZABBIX客户端(代理)
查看>>
CentOS命令登录MySQL时,报错ERROR 1045 (28000)
查看>>
jsp下拉框中显示数据库信息&&jsp 下拉框从数据库中如何取值?
查看>>
Linux系统编程 --- 共享内存及内存映射【十全十美】
查看>>
如何创建一个swap文件
查看>>
mysql联合索引
查看>>
我的友情链接
查看>>
H5页面快速搭建之高级字体应用实践
查看>>