Java – Convert IP address to Decimal Number

朱雀 2022-06-08 07:13 280阅读 0赞

1. IP Address to Decimal

We show you two ways to convert an IP address to a decimal number

  • Normal power of 256
  • Bit shifting

1.1 First Example – Power of 256

The IP address is “base 256”, to convert 192.168.1.2 to decimal (base 10) the formula is:

192 x (256)^3 + 168 x (256)^2 + 1 x (256)^1 + 2 (256)^0 = ?
3221225472 + 11010048 + 256 + 2 = 3232235778

  1. public long ipToLong(String ipAddress) {
  2. String[] ipAddressInArray = ipAddress.split("\\.");
  3. long result = 0;
  4. for (int i = 0; i < ipAddressInArray.length; i++) {
  5. int power = 3 - i;
  6. int ip = Integer.parseInt(ipAddressInArray[i]);
  7. result += ip * Math.pow(256, power);
  8. }
  9. return result;
  10. }

Some developers prefer to use modular like this

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

1.2 Second Example - Bit shifting

Review the binary bit shifting graph below :

  1. public long ipToLong(String ipAddress) {
  2. long result = 0;
  3. String[] ipAddressInArray = ipAddress.split("\\.");
  4. for (int i = 3; i >= 0; i--) {
  5. long ip = Long.parseLong(ipAddressInArray[3 - i]);
  6. //left shifting 24,16,8,0 and bitwise OR
  7. //1. 192 << 24
  8. //1. 168 << 16
  9. //1. 1 << 8
  10. //1. 2 << 0
  11. result |= ip << (i * 8);
  12. }
  13. return result;
  14. }
  15. 192 00000000 00000000 00000000 11000000 -----------------------------------------------
  16. 192 << 24 11000000 00000000 00000000 00000000
  17. Result 00000000 00000000 00000000 00000000
  18. Result |= 11000000 00000000 00000000 00000000
  19. 168 00000000 00000000 00000000 10101000 -----------------------------------------------
  20. 168 << 16 00000000 10101000 00000000 00000000
  21. Result 11000000 00000000 00000000 00000000
  22. Result |= 11000000 10101000 00000000 00000000
  23. 1 00000000 00000000 00000000 00000001 -----------------------------------------------
  24. 1 << 8 00000000 00000000 00000001 00000000
  25. Result 11000000 10101000 00000000 00000000
  26. Result |= 11000000 10101000 00000001 00000000
  27. 2 00000000 00000000 00000000 00000010 -----------------------------------------------
  28. 2 << 0 00000000 00000000 00000000 00000010
  29. Result 11000000 10101000 00000001 00000000
  30. Result |= 11000000 10101000 00000001 00000010

Convert the final binary code to decimal, by hand calculation :) ~

  1. 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. Decimal to IP Address

We show you two bit shifting and “0xff” masking examples to convert a decimal number back to IP address. The bit shifting is very hard to explain in words, it’s better review the binary flows below :

2.1 First Example.

  1. //ip = 3232235778
  2. public String longToIp(long ip) {
  3. StringBuilder result = new StringBuilder(15);
  4. for (int i = 0; i < 4; i++) {
  5. result.insert(0,Long.toString(ip & 0xff));
  6. if (i < 3) {
  7. sb.insert(0,'.');
  8. }
  9. ip = ip >> 8;
  10. }
  11. return result.toString();
  12. }

Review the bit shifting flows :

  1. 3232235778 11000000 10101000 00000001 00000010
  2. <<Loop 1>> -----------------------------------------------------------
  3. ip 11000000 10101000 00000001 00000010
  4. & 0xff 00000000 00000000 00000000 11111111
  5. Result 00000000 00000000 00000000 00000010 = 2
  6. Result Append .2
  7. -------------------------> 8
  8. ip >> 8 00000000 11000000 10101000 00000001 {off 00000010}
  9. <<Loop 2>> -----------------------------------------------------------
  10. ip 00000000 11000000 10101000 00000001
  11. & 0xff 00000000 00000000 00000000 11111111
  12. Result 00000000 00000000 00000000 00000001 = 1
  13. Result Append 1.2
  14. ----------------> 8
  15. ip >> 8 00000000 00000000 11000000 10101000 {off 00000001}
  16. <<Loop 3>> -----------------------------------------------------------
  17. ip 00000000 00000000 11000000 10101000
  18. & 0xff 00000000 00000000 00000000 11111111
  19. Result 00000000 00000000 00000000 10101000 = 168
  20. Result Append 168.1.2
  21. -------> 8
  22. ip >> 8 00000000 00000000 00000000 11000000 {off 10101000}
  23. <<Loop 4>> -----------------------------------------------------------
  24. ip 00000000 00000000 00000000 11000000
  25. & 0xff 00000000 00000000 00000000 11111111
  26. Result 00000000 00000000 00000000 11000000 = 192
  27. Result Append 192.168.1.2
  28. -----------> 8
  29. ip >> 8 00000000 00000000 00000000 00000000 {off 11000000}

2.2 Second Example.

  1. //ip = 3232235778
  2. public String longToIp(long ip) {
  3. return ((ip >> 24) & 0xFF) + "."
  4. + ((ip >> 16) & 0xFF) + "."
  5. + ((ip >> 8) & 0xFF) + "."
  6. + (ip & 0xFF);
  7. }
  8. 3232235778 11000000 10101000 00000001 00000010
  9. 1. (ip >> 24) & 0xFF -----------------------------------------------------------
  10. ip 11000000 10101000 00000001 00000010
  11. -------------------------------------> 24
  12. ip >> 24 00000000 00000000 00000000 11000000 {off 10101000 00000001 00000010}
  13. & 0xff 00000000 00000000 00000000 11111111
  14. Result 00000000 00000000 00000000 11000000 = 192
  15. 2. (ip >> 16) & 0xFF -----------------------------------------------------------
  16. ip 11000000 10101000 00000001 00000010
  17. -------------------------------------> 16
  18. ip >> 16 00000000 00000000 11000000 10101000 {off 00000001 00000010}
  19. & 0xff 00000000 00000000 00000000 11111111
  20. Result 00000000 00000000 00000000 10101000 = 168
  21. 3. (ip >> 8) & 0xFF -----------------------------------------------------------
  22. ip 11000000 10101000 00000001 00000010
  23. --------------------------------------> 8
  24. ip >> 24 00000000 11000000 10101000 00000001 {off 00000010}
  25. & 0xff 00000000 00000000 00000000 11111111
  26. Result 00000000 00000000 00000000 00000001 = 1
  27. 4. ip & 0xFF -----------------------------------------------------------
  28. ip 11000000 10101000 00000001 00000010
  29. & 0xff 00000000 00000000 00000000 11111111
  30. Result 00000000 00000000 00000000 00000010 = 2

3. Java Source Code

Full Java example to demonstrate above scenario :

  1. package com.mkyong.core;
  2. public class JavaBitwiseExample {
  3. public static void main(String[] args) {
  4. JavaBitwiseExample obj = new JavaBitwiseExample();
  5. System.out.println("iptoLong : " + obj.ipToLong("192.168.1.2"));
  6. System.out.println("iptoLong2 : " + obj.ipToLong2("192.168.1.2"));
  7. System.out.println("longToIp : " + obj.longToIp(3232235778L));
  8. System.out.println("longToIp2 : " + obj.longToIp2(3232235778L));
  9. }
  10. // example : 192.168.1.2
  11. public long ipToLong(String ipAddress) {
  12. // ipAddressInArray[0] = 192
  13. String[] ipAddressInArray = ipAddress.split("\\.");
  14. long result = 0;
  15. for (int i = 0; i < ipAddressInArray.length; i++) {
  16. int power = 3 - i;
  17. int ip = Integer.parseInt(ipAddressInArray[i]);
  18. // 1. 192 * 256^3
  19. // 2. 168 * 256^2
  20. // 3. 1 * 256^1
  21. // 4. 2 * 256^0
  22. result += ip * Math.pow(256, power);
  23. }
  24. return result;
  25. }
  26. public long ipToLong2(String ipAddress) {
  27. long result = 0;
  28. String[] ipAddressInArray = ipAddress.split("\\.");
  29. for (int i = 3; i >= 0; i--) {
  30. long ip = Long.parseLong(ipAddressInArray[3 - i]);
  31. // left shifting 24,16,8,0 and bitwise OR
  32. // 1. 192 << 24
  33. // 1. 168 << 16
  34. // 1. 1 << 8
  35. // 1. 2 << 0
  36. result |= ip << (i * 8);
  37. }
  38. return result;
  39. }
  40. public String longToIp(long i) {
  41. return ((i >> 24) & 0xFF) +
  42. "." + ((i >> 16) & 0xFF) +
  43. "." + ((i >> 8) & 0xFF) +
  44. "." + (i & 0xFF);
  45. }
  46. public String longToIp2(long ip) {
  47. StringBuilder sb = new StringBuilder(15);
  48. for (int i = 0; i < 4; i++) {
  49. // 1. 2
  50. // 2. 1
  51. // 3. 168
  52. // 4. 192
  53. sb.insert(0, Long.toString(ip & 0xff));
  54. if (i < 3) {
  55. sb.insert(0, '.');
  56. }
  57. // 1. 192.168.1.2
  58. // 2. 192.168.1
  59. // 3. 192.168
  60. // 4. 192
  61. ip = ip >> 8;
  62. }
  63. return sb.toString();
  64. }
  65. /* 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)); } */
  66. }

OUTPUT

  1. iptoLong : 3232235778
  2. iptoLong2 : 3232235778
  3. longToIp : 192.168.1.2
  4. longToIp2 : 192.168.1.2

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

发表评论

表情:
评论列表 (有 0 条评论,280人围观)

还没有评论,来说两句吧...

相关阅读