(C #) Converting IP addresses and digital addresses,

Source: Internet
Author: User

(C #) Converting IP addresses and digital addresses,

Webmaster IP Address: http://tool.chinaz.com/ip/

And a tool address for converting IP addresses to numbers: http://www.msxindl.com/tools/ip/ip_num.asp

As you can see, the IP address query tool converts the IP segment address into a digital address before obtaining the physical IP address.
Certificate -------------------------------------------------------------------------------------------------------------------------------------
Why do I need to convert an IP address segment to a digital address?

According to TCP/IP protocol, IP addresses are composed of 32-bit binary numbers and are unique over the INTERNET. For example, the IP address of a computer connected to the Internet is:
11010010 01001001 10001100 00000010
Obviously, these numbers are not very memorable. In order to facilitate memory, people divide the 32-bit binary code that makes up the IP address of a computer into four segments, each segment has eight digits, separated by decimal places, and converted each eight-bit binary code into decimal digits, in this way, the IP address of the above computer becomes 118.123.15.102.
Because the IP segments in the same region are very similar, it is very troublesome to directly compare the IP segments (118.123.15.102), and data storage is not easy to implement, therefore, the IP address is converted to a digital address, and the physical address of the IP address segment is determined.

Certificate -------------------------------------------------------------------------------------------------------------------------------------
After understanding the concept of IP, how can I convert an IP address (118.123.15.102) to a digital address?

The IP address is 32 in binary format and is converted to four decimal segments for convenience of memory. Therefore, you only need to convert the IP address to binary and convert it to decimal format to get the IP address.

. Net c # mutual conversion between IP addresses and digital addresses:

// Convert the IP address to a digital address
Public
 Static UintIPToInt (StringIpAddress)
{
StringDisjunctiveStr =".,:";
Char[] Delimiter = disjunctiveStr. ToCharArray ();
String[] StartIP =Null;
For(IntI = 1; I <= 5; I ++)
{
StartIP = ipAddress. Split (delimiter, I );
}
StringA1 = startIP [0]. ToString ();
StringA2 = startIP [1]. ToString ();
StringA3 = startIP [2]. ToString ();
StringA4 = startIP [3]. ToString ();
UintU1 =Uint. Parse (a1 );
UintU2 =Uint. Parse (a2 );
UintU3 =Uint. Parse (a3 );
UintU4 =Uint. Parse (a4 );

UintU = U1 <24;
U + = U2 <16;
U + = U3 <8;
U + = U4;
ReturnU;
}

// Convert a digital address to an IP address
Public Static StringIntToIP (UintIpAddress)
{
LongUi1 = ipAddress & 0xFF000000;
Ui1 = ui1> 24;
LongUi2 = ipAddress & 0x00FF0000;
Ui2 = ui2> 16;
LongUi3 = ipAddress & 0x0000FF00;
Ui3 = ui3> 8;
LongUi4 = ipAddress & 0x000000FF;
StringIPstr ="";
IPstr = System. Convert. ToString (ui1) +"."
+ System. Convert. ToString (ui2) +"."
+ System. Convert. ToString (ui3)
+"."+ System. Convert. ToString (ui4 );
ReturnIPstr;
}
After the address is converted to a numeric address, It is very convenient to query it. As long as the IP address is in a certain segment, you can find the specific physical address. Of course, the premise is that there is a huge IP address library. There is also an IP database on hand, which has more than records.



========================================================== ========================================================== ===
Where can I convert an IP segment to a real physical address?

There are many functions,
1. For example, traffic statistics,
2. The geographical location in the I/M chat tool is displayed, and the geographical location on the webpage is displayed,
3. portal websites are differentiated by cities. the visitor's IP address is used to determine which city the Visitor should visit,
4. Some weather forecast websites use the city where visitors are located by default.

 

PS:

Today, my friend asked me if I had used a website with IP address restrictions, saying that it was necessary to restrict access to only one city, and access from other cities was denied.

At first, I thought that if I only had some restrictions, I could directly set them on the server, but the whole city would probably be restricted by querying the IP address library through code.

But when I think of the IP address library, I want to find the IP address and compare it with the city. Well, it seems very troublesome, but I can't think of other methods.

This is my friend who gave me two numbers, indicating the starting IP address and ending IP address for access.

At first glance, how can the IP address be a number?

So I searched the internet to know that the original IP address can be converted to a number.

In the first article, we found that the Ping command can use numbers instead of IP addresses, that is, a long integer number can be used to represent IP addresses. The article says this:

 

 

Digit string instead of IP Address

In the Ping command, you can also use a number string to replace the IP address. Do you believe this? Run the "ping 3658906394" command and you will see the returned information of the IP address "218.22.123.26.

Why? In fact, "3658906394" is another representation of the IP address "218.22.123.26. Of course, you can Ping other IP addresses in the same way.

How is a string converted? In fact, it is not complicated. Take the IP address "218.22.123.26" as an example. The method for converting an IP address to a number string is as follows: Convert "218.22.123.26" to "DA.16.7B" in hexadecimal format. 1A ", then remove the decimal point, change to" DA167B1A ", and finally convert the hexadecimal number to" 3658906394 ", then" 218.22.123.26 "is changed to" 3658906394. The same method is used to convert other IP addresses to strings.

Tip: In some LAN environments, the "Ping + digit string" command may fail and the message "Unknown host digit string" appears ", this is because the numeric string is resolved to the host name rather than the IP address.

 

 

Convert the four segments of the IP address to hexadecimal respectively, and then convert the hexadecimal number to decimal. It seems very troublesome to find another one, and the other one will be more direct, the content on the csdn forum is as follows:

 

 

If the IP address is 202.106.185.77, it is converted to a decimal number and then 3395991885. How can this problem be solved? Are decimal numbers such as 202 and 106 no longer ????

 

 

202*255*255*255 + 106*255*255 + 185*255 + 77

 

 

 

I know how to convert an IP address into a number, but it seems a lot of trouble to convert a number into an IP address. See if there are any ready-made functions that can be converted. The Code is as follows:

 

 

[Java]Download plaincopy

 

  1. /**
  2. * The IP address is converted to an integer.
  3. * @ Param ip
  4. * @ Return
  5. */
  6. Public static long ip2long (String ip ){
  7. String [] ips = ip. split ("[.]");
  8. Long num = 16777216L * Long. parseLong (ips [0]) + 65536L * Long. parseLong (ips [1]) + 256 * Long. parseLong (ips [2]) + Long. parseLong (ips [3]);
  9. Return num;
  10. }
  11. /**
  12. * Convert an integer to an IP address.
  13. * @ Param ipLong
  14. * @ Return
  15. */
  16. Public static String long2ip (long ipLong ){
  17. // Long ipLong = 1037591503;
  18. Long mask [] = {0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 };
  19. Long num = 0;
  20. StringBuffer ipInfo = new StringBuffer ();
  21. For (int I = 0; I <4; I ++ ){
  22. Num = (ipLong & mask [I])> (I * 8 );
  23. If (I> 0) ipInfo. insert (0 ,".");
  24. IpInfo. insert (0, Long. toString (num, 10 ));
  25. }
  26. Return ipInfo. toString ();
  27. }

 

 

The Java code is still used. You can change it to asp and asp.net.

Later I thought about it. I don't need to convert numbers into IP addresses. I just need to convert the client's IP address into a growth integer, and then compare it with the long integers at both ends to see if it is in this city, the IP address library does not need to be accessed.

Link: http://goolgeplus.lofter.com/post/8de0d_d6d0a

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.