Java –将IP地址转换为十进制数

本文详细介绍了如何在Java中将IP地址转换为十进制数字,以及相反的转换过程。提供了两种方法,包括使用256的幂次方和位移操作,并展示了如何将十进制数字转换回IP地址。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java-ip-to-decimal

在本教程中,我们向您展示如何将IP地址转换为Java中的十进制等效值,反之亦然。 举些例子 :

255.255.255.255  <->  4294967295
192.168.1.2      <->  3232235778

1. IP地址到十进制

我们向您展示了两种将IP地址转换为十进制数字的方法

  1. 普通功率256
  2. 移位

1.1第一个示例– 256的幂
IP地址为“ base 256”,将192.168.1.2转换为十进制(base 10),公式为:

192 x (256)^3 + 168 x (256)^2 + 1 x (256)^1 + 2 (256)^0 = ?
3221225472 + 11010048 + 256 + 2 = 3232235778
public long ipToLong(String ipAddress) {

	String[] ipAddressInArray = ipAddress.split("\\.");

	long result = 0;
	for (int i = 0; i < ipAddressInArray.length; i++) {

		int power = 3 - i;
		int ip = Integer.parseInt(ipAddressInArray[i]);
		result += ip * Math.pow(256, power);

	}

	return result;
  }

一些开发人员更喜欢这样使用模块化

result += (Integer.parseInt(ipAddressInArray[i]) % 256 * Math.pow(256, power));

1.2第二个例子–移位
查看下面的二进制位移图:

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. 192 << 24
		//1. 168 << 16
		//1. 1   << 8
		//1. 2   << 0
		result |= ip << (i * 8);
		
	}

	return result;
  }
192         00000000 00000000 00000000 11000000 
-----------------------------------------------
192 << 24   11000000 00000000 00000000 00000000 
Result      00000000 00000000 00000000 00000000 
Result |=   11000000 00000000 00000000 00000000 

168         00000000 00000000 00000000 10101000 
-----------------------------------------------
168 << 16   00000000 10101000 00000000 00000000 
Result	    11000000 00000000 00000000 00000000 
Result |=   11000000 10101000 00000000 00000000

1           00000000 00000000 00000000 00000001 
-----------------------------------------------
1   << 8    00000000 00000000 00000001 00000000 
Result	    11000000 10101000 00000000 00000000 
Result |=   11000000 10101000 00000001 00000000

2           00000000 00000000 00000000 00000010 
-----------------------------------------------
2 << 0      00000000 00000000 00000000 00000010 
Result	    11000000 10101000 00000001 00000000 
Result |=   11000000 10101000 00000001 00000010

通过手工计算将最终的二进制代码转换为十进制🙂〜

Result      11000000 10101000 00000001 00000010
index       0 - 31, start from right.
      	    31(1),30(1),29,28,27,26,25,24,23(1),22,21(1),20,19(1),18,17,16,15,14,13,12,11,10,9,8(1),7,6,5,4,3,2,1(1),0
Decimal     1x2^31 + 1x2^30 + 1x2^23 + 1x2^21 + 1x2^19 + 1x2^8 + 1x2^1
            2147483648 + 1073741824 + 8388608 + 2097152 + 524288 + 256 + 2
            3232235778

2.十进制到IP地址

我们向您展示了两个移位和“ 0xff”掩码示例,这些示例将十进制数转换回IP地址。 移位很难用语言来解释,最好回顾一下下面的二进制流:

2.1第一个例子。

//ip = 3232235778
  public String longToIp(long ip) {
	StringBuilder result = new StringBuilder(15);

	for (int i = 0; i < 4; i++) {
		
		result.insert(0,Long.toString(ip & 0xff));

		if (i < 3) {
			sb.insert(0,'.');
		}

		ip = ip >> 8;
	}
	return result.toString();
  }

查看位移流:

3232235778		11000000 10101000 00000001 00000010

<<Loop 1>>
-----------------------------------------------------------
ip      		11000000 10101000 00000001 00000010 
& 0xff			00000000 00000000 00000000 11111111 
Result                  00000000 00000000 00000000 00000010 = 2
Result Append           .2
                                 -------------------------> 8
ip >> 8			00000000 11000000 10101000 00000001 {off 00000010}

<<Loop 2>>
-----------------------------------------------------------
ip      		00000000 11000000 10101000 00000001
& 0xff			00000000 00000000 00000000 11111111 
Result                  00000000 00000000 00000000 00000001 = 1
Result Append           1.2
                                          ----------------> 8
ip >> 8			00000000 00000000 11000000 10101000 {off 00000001}

<<Loop 3>>
-----------------------------------------------------------
ip      		00000000 00000000 11000000 10101000
& 0xff			00000000 00000000 00000000 11111111 
Result                  00000000 00000000 00000000 10101000 = 168
Result Append           168.1.2
                                                   -------> 8
ip >> 8			00000000 00000000 00000000 11000000 {off 10101000}

<<Loop 4>>
-----------------------------------------------------------
ip      		00000000 00000000 00000000 11000000
& 0xff			00000000 00000000 00000000 11111111 
Result                  00000000 00000000 00000000 11000000 = 192
Result Append           192.168.1.2
                                                            -----------> 8
ip >> 8			00000000 00000000 00000000 00000000 {off 11000000}

2.2第二个例子。

//ip = 3232235778
  public String longToIp(long ip) {

	return ((ip >> 24) & 0xFF) + "." 
		+ ((ip >> 16) & 0xFF) + "." 
		+ ((ip >> 8) & 0xFF) + "." 
		+ (ip & 0xFF);

  }
3232235778		11000000 10101000 00000001 00000010

1. (ip >> 24) & 0xFF
-----------------------------------------------------------
ip      		11000000 10101000 00000001 00000010 
                                                   -------------------------------------> 24
ip >> 24                00000000 00000000 00000000 11000000 {off 10101000 00000001 00000010}  
& 0xff			00000000 00000000 00000000 11111111 
Result                  00000000 00000000 00000000 11000000 = 192

2. (ip >> 16) & 0xFF
-----------------------------------------------------------
ip      		11000000 10101000 00000001 00000010 
                                          -------------------------------------> 16
ip >> 16                00000000 00000000 11000000 10101000 {off 00000001 00000010}  
& 0xff			00000000 00000000 00000000 11111111 
Result                  00000000 00000000 00000000 10101000 = 168

3. (ip >> 8) & 0xFF
-----------------------------------------------------------
ip      		11000000 10101000 00000001 00000010 
                                 --------------------------------------> 8
ip >> 24                00000000 11000000 10101000 00000001 {off 00000010}  
& 0xff			00000000 00000000 00000000 11111111 
Result                  00000000 00000000 00000000 00000001 = 1

4. ip & 0xFF
-----------------------------------------------------------
ip      		11000000 10101000 00000001 00000010 
& 0xff			00000000 00000000 00000000 11111111 
Result                  00000000 00000000 00000000 00000010 = 2

3. Java源代码

完整的Java示例来演示上述场景:

package com.mkyong.core;

public class JavaBitwiseExample {

	public static void main(String[] args) {

		JavaBitwiseExample obj = new JavaBitwiseExample();

		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] = 192
		String[] ipAddressInArray = 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. 192 << 24
			// 1. 168 << 16
			// 1. 1 << 8
			// 1. 2 << 0
			result |= ip << (i * 8);

		}

		return result;
	}

	public String longToIp(long i) {

		return ((i >> 24) & 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. 168
			// 4. 192
			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. 192
			ip = ip >> 8;

		}

		return sb.toString();
	}

	/*
	private static void printPrettyBinary(String binary) {

		String s1 = String.format("%32s", binary).replace(' ', '0');

		System.out.format("%8s %8s %8s %8s %n", 
			s1.substring(0, 8), 
			s1.substring(8, 16), 
			s1.substring(16, 24), 
			s1.substring(24, 32));
	}
	*/
}

输出量

iptoLong  : 3232235778
iptoLong2 : 3232235778
longToIp  : 192.168.1.2
longToIp2 : 192.168.1.2

参考文献

  1. Java和0xff示例
  2. 按位或示例
  3. 将基数10转换为IP
  4. Microsoft:IP地址
  5. 维基百科:两个的幂
  6. 维基百科:按位运算
  7. Stackoverflow:IP地址转换为十进制,反之亦然
  8. Stackoverflow:位移的绝对入门指南?

翻译自: https://mkyong.com/java/java-convert-ip-address-to-decimal-number/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值