Java converts IP addresses to numbers and reverse conversions (bitwise operations and 0xFF use) second season

Source: Internet
Author: User
Tags bitwise

Original:

http://www.mkyong.com/java/java-convert-ip-address-to-decimal-number/


From the same blog.


IP to Digital (the second algorithm.) Move with left, bitwise, or implemented. Efficiency is higher than the previous one. ):

Public long Iptolong (String IPAddress) {
 
	long result = 0;
 
	string[] Ipaddressinarray = Ipaddress.split ("\;");
 
	for (int i = 3; I >= 0; i--) {
 
		Long IP = Long.parselong (ipaddressinarray[3-i]);
 
		Left shifting 24,16,8,0 and bitwise OR
 
		//1. <<//1.
		1   << 8
		/ /1. 2   << 0 result
		|= IP << (i * 8);
 
	}
	return result;
  }


Digital to IP, both algorithms are similar:

  IP = 3232235778 public
  String longtoip (long IP) {
	StringBuilder result = new StringBuilder;
 
	for (int i = 0; i < 4; i++) {
 
		Result.insert (0,long.tostring (IP & 0xff));
 
		if (I < 3) {
			result.insert (0, '. ');
		}
 
		IP = IP >> 8;
	}
	return result.tostring ();
  }

  IP = 3232235778 public
  String longToIp2 (long IP) {return
 
	(IP >>) & 0xFF) + "." 
		+ ((IP >>) & 0xFF) + "." 
		+ ((IP >> 8) & 0xFF) + "." 
		+ (IP & 0xFF);
  }


Complete code:

public class Javabitwiseexample {public static void main (string[] args) {javabitwiseexample obj = new Javabitwise
 
		Example ();
		System.out.println ("Iptolong:" + obj.iptolong ("192.168.1.2"));
 
		System.out.println ("iptoLong2:" + obj.iptolong2 ("192.168.1.2"));
		System.out.println ("Longtoip:" + Obj.longtoip (3232235778L));
 
	System.out.println ("LONGTOIP2:" + obj.longtoip2 (3232235778L)); }//example:192.168.1.2 public long Iptolong (String ipaddress) {//ipaddressinarray[0] = string[] IPAddr
 
		Essinarray = Ipaddress.split ("\.");
		Long result = 0;
			for (int i = 0; i < ipaddressinarray.length i++) {int power = 3-i;
 
			int ip = integer.parseint (ipaddressinarray[i]); 1. 192 * 256^3//2. 168 * 256^2//3. 1 * 256^1//4.
 
		2 * 256^0 Result + = IP * MATH.POW (256, power);
 
	return result;
 
		Public long IpToLong2 (String ipaddress) {long result = 0;
 
		string[] Ipaddressinarray = Ipaddress.split ("\;");for (int i = 3; I >= 0; i--) {Long IP = Long.parselong (ipaddressinarray[3-i]); Left shifting 24,16,8,0 and bitwise OR//1. << 24//1. << 16//1. 1 << 8//1.
 
		2 << 0 Result |= IP << (i * 8);
	return result;  Public String Longtoip (long i) {return ((I >>) & 0xFF) + "." + ((I >> 16)
 
	& 0xFF) + "." + ((I >> 8) & 0xFF) + "." + (I & 0xFF);
 
		Public String longToIp2 (long IP) {StringBuilder sb = new StringBuilder (15); for (int i = 0; i < 4; i++) {//1.2/2.1//3.//4. Sb.insert (0, long.tostring (IP &
 
			0xFF));
			if (I < 3) {Sb.insert (0, '. '); }//1. 192.168.1.2//2. 192.168.1//3. 192.168//4.
 
		The IP = IP >> 8;
	return sb.tostring (); }
}

Output:

Iptolong  : 3232235778
iptolong2:3232235778
longtoip  : 192.168.1.2
longtoip2:192.168.1.2


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.