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. }
时间: 2025-01-10 10:33:25

Convert IPv6 Address to IP numbers (C#)的相关文章

[LeetCode] Validate IP Address 验证IP地址

In this problem, your job to write a function to check whether a input string is a valid IPv4 address or IPv6 address or neither. IPv4 addresses are canonically represented in dot-decimal notation, which consists of four decimal numbers, each ranging

IPv6 tutorial 4 IPv6 address syntax

https://4sysops.com/archives/ipv6-tutorial-part-4-ipv6-address-syntax/ Now that you know about the new features of IPv6, it is time to have a closer look at the practical details. In this post, I will give a short summary about the IPv6 address synta

How to support both ipv4 and ipv6 address for JAVA code.

IPv6 have colon character, for example FF:00::EEIf concatenate URL String, IPv6 URL will like: http://FF:00::EE:8888/a/b/cActually we want: http://[FF:00::EE]:8888/a/b/cSimply concatenate URL will cause error. Take above line for example. If not use

[Leetcode] restore ip address 存储IP地址

Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example:Given"25525511135", return["255.255.11.135", "255.255.111.35"]. (Order does not matter) 题意:给定一由纯数字组成的字符串,以

Window配置网络设定IPv6的固定IP自动被修改为169.254.*.*的问题

一.问题现象 前几天遇见一个比较奇怪的问题,一台局域网内的电脑,之前一直是手动设定的固定IP(192.168.8.120),电脑进入待机之后,再次打开发现内网连不通了. 二.问题定位 1.首先ping了下网关(192.168.8.1),发现ping不通. 2.ipconfig看了下ip设置,发现该网卡的地址编程169.254.*.*. 3.通过网络管理重新修改IP,重启适配器,发现还是169.254.*.*的IP,在详细信息中可以看见自己设定的IP. 4.设定为DHCP自动获取IP,自动获取一个

Shorten IPv6 Address

题目链接 题意:转换一个128位的二进制串,变成16进制,并且格式为x:x:x:x:x:x:x:x然后多个相邻的0变成::,且只有一个::,求变成的字典序最小且最短的字符串. 思路:大水题,但是就是一直wa,思路都一样的还是wa.先将二进制转化为十进制,然后用%x输出,因为连续的0可变短,但有优先级,首先选0长的,然后如果长度相等,先选中间,中间里面先选中间偏后,然后是最后,最后是前面.差不多就这意思吧. #include<cstdio> #include<cstring> #in

Leetcode: Validate IP Address

In this problem, your job to write a function to check whether a input string is a valid IPv4 address or IPv6 address or neither. IPv4 addresses are canonically represented in dot-decimal notation, which consists of four decimal numbers, each ranging

468. Validate IP Address

Write a function to check whether an input string is a valid IPv4 address or IPv6 address or neither. IPv4 addresses are canonically represented in dot-decimal notation, which consists of four decimal numbers, each ranging from 0 to 255, separated by

[Swift]LeetCode468. 验证IP地址 | Validate IP Address

Write a function to check whether an input string is a valid IPv4 address or IPv6 address or neither. IPv4 addresses are canonically represented in dot-decimal notation, which consists of four decimal numbers, each ranging from 0 to 255, separated by