Vote statistics-convert an IP address to a Long integer

Source: Internet
Author: User

The two statements we see in the voting filter: String ip = request. getRemoteAddr (); and long ipnum = StringHandler. getIpNum (ip); the first one is to obtain the ip address of the client, and the second statement is to convert the obtained ip address into a growth integer. Here, the static method getIpNum (String ip) in the StringHandler class is called. Let's take a look at how this is implemented:


[Java]
Public class StringHandler {
Public static long getIpNum (String ip ){
Long ipNum = 0;
If (ip! = Null &&! Ip. equals ("")){
String [] subips = ip. split ("\\.");
For (int I = 0; I <subips. length; I ++ ){
IpNum + = Integer. parseInt (subips [I]);
If (I <subips. length-1)
IpNum = ipNum <8;
}
}
Return ipNum;
}
}

Public class StringHandler {
Public static long getIpNum (String ip ){
Long ipNum = 0;
If (ip! = Null &&! Ip. equals ("")){
String [] subips = ip. split ("\\.");
For (int I = 0; I <subips. length; I ++ ){
IpNum + = Integer. parseInt (subips [I]);
If (I <subips. length-1)
IpNum = ipNum <8;
}
}
Return ipNum;
}
} Note: ipNum = ipNum <8; shifts the binary number of ipNum eight places to the left.

 

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.