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.

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

 

目录
相关文章
|
9月前
|
数据采集 API 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK使用ForceIP强制修改网口IP功能(C#)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK使用ForceIP强制修改网口IP功能(C#)
72 0
|
9月前
|
JavaScript 前端开发 C#
C# webbrowser控件设置代理IP访问网站
C# webbrowser控件设置代理IP访问网站
455 5
|
小程序 前端开发 安全
【C#】 MVC4 开发小程序-实现人脸识别-本地和手机预览使用IP测试
小程序Camera组件拍照上传图片到指定的服务器(本地或者外网的IP服务器),然后C# MVC后台调用百度人脸识别接口,实现人脸识别功能呢
299 0
|
网络协议 C# 移动开发
c#获取和设置网卡ip/dns等信息
  using System; using System.Windows.Forms; using System.Management; using System.
1590 0
|
C# 负载均衡 域名解析
c#中HttpWebRequest使用Proxy实现指定IP的域名请求
我有这么一个需求: 一个域名,xxx.com,它后面其实有很多个iP:比如: 1.2.3.4,5.6.7.8,9.10.11.12这些ip上面都有同样的网站,域名解析的时候会随机分配一个ip给你(这个就是DNS负载均衡)。
3597 0
|
C#
C# 设置IP地址及设置自动获取IP
原文:C# 设置IP地址及设置自动获取IP 1.添加引用&quot;system.Management&quot; 2.添加using System.Management using System; using System.
2765 0
|
C#
C#获取本机IP搜集整理7种方法
① 1 private void GetIP() 2 { 3 string hostName = Dns.GetHostName();//本机名 4 //System.
848 0
|
C#
C# .net通过域名获取IP(转)
        IPAddress[] IPs = Dns.GetHostAddresses("www.by84.com");        foreach (IPAddress ip in IPs)        {           Response.
1505 0
|
3月前
|
C# 开发者
C# 一分钟浅谈:Code Contracts 与契约编程
【10月更文挑战第26天】本文介绍了 C# 中的 Code Contracts,这是一个强大的工具,用于通过契约编程增强代码的健壮性和可维护性。文章从基本概念入手,详细讲解了前置条件、后置条件和对象不变量的使用方法,并通过具体代码示例进行了说明。同时,文章还探讨了常见的问题和易错点,如忘记启用静态检查、过度依赖契约和性能影响,并提供了相应的解决建议。希望读者能通过本文更好地理解和应用 Code Contracts。
60 3