How to convert IP addresses and integers in JAVA

Source: Internet
Author: User

How to convert IP addresses and integers in JAVA

This example describes how to convert IP addresses and integers in JAVA. Share it with you for your reference. The specific analysis is as follows:

I. Basic knowledge points

IP --> INTEGER:

Convert IP addresses to byte Arrays

Convert to int through the left shift (<), and (&), or (|) Operations

Integer --> IP:

Perform the right shift operation (>>>) on the integer, shift the integer to the right by 24 bits, and then perform the operation with the operator (&) 0xFF. The obtained number is the first IP address.

Perform the right shift operation (>>>) on the integer, shift 16 bits to the right, and then perform the operation with the operator (&) 0xFF. The resulting number is the second segment of the IP address.

Perform the right shift operation (>>>) on the integer, shift the integer to the right by 8 digits, and then perform the operation with the operator (&) 0xFF. The obtained number is the third segment of the IP address.

Perform the integer value and operator (&) 0xFF. The obtained number is the fourth IP address.

Ii. java code example (IPv4Util. java)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

Package michael. utils;

Import java.net. InetAddress;

Public class implements 4util {

Private final static int INADDRSZ = 4;

Public static byte [] ipToBytesByInet (String ipAddr ){

Try {

Return InetAddress. getByName (ipAddr). getAddress ();

} Catch (Exception e ){

Throw new IllegalArgumentException (ipAddr + "is invalid IP ");

}

} JTA practice: Spring + ATOMIKOS

Public static byte [] ipToBytesByReg (String ipAddr ){

Byte [] ret = new byte [4];

Try {

String [] ipArr = ipAddr. split ("\\.");

Ret [0] = (byte) (Integer. parseInt (ipArr [0]) & 0xFF );

Ret [1] = (byte) (Integer. parseInt (ipArr [1]) & 0xFF );

Ret [2] = (byte) (Integer. parseInt (ipArr [2]) & 0xFF );

Ret [3] = (byte) (Integer. parseInt (ipArr [3]) & 0xFF );

Return ret;

} Catch (Exception e ){

Throw new IllegalArgumentException (ipAddr + "is invalid IP ");

}

}

Public static String bytesToIp (byte [] bytes ){

Return new StringBuffer (). append (bytes [0] & 0xFF). append ('.'). append (

Bytes [1] & 0xFF). append ('.'). append (bytes [2] & 0xFF)

. Append ('.'). append (bytes [3] & 0xFF). toString ();

}

Public static int bytesToInt (byte [] bytes ){

Int addr = bytes [3] & 0xFF;

Addr | = (bytes [2] <8) & 0xFF00 );

Addr | = (bytes [1] <16) & 0xFF0000 );

Addr | = (bytes [0] <24) & 0xFF000000 );

Return addr;

}

Public static int ipToInt (String ipAddr ){

Try {

Return bytesToInt (ipToBytesByInet (ipAddr ));

} Catch (Exception e ){

Throw new IllegalArgumentException (ipAddr + "is invalid IP ");

}

}

Public static byte [] intToBytes (int ipInt ){

Byte [] ipAddr = new byte [INADDRSZ];

IpAddr [0] = (byte) (ipInt >>> 24) & 0xFF );

IpAddr [1] = (byte) (ipInt >>> 16) & 0xFF );

IpAddr [2] = (byte) (ipInt >>> 8) & 0xFF );

IpAddr [3] = (byte) (ipInt & 0xFF );

Return ipAddr;

}

Public static String intToIp (int ipInt ){

Return new StringBuilder (). append (ipInt> 24) & 0xff). append ('.')

. Append (ipInt> 16) & 0xff). append ('.'). append (

(IpInt> 8) & 0xff). append ('.'). append (ipInt & 0xff ))

. ToString ();

}

Public static int [] getIPIntScope (String ipAndMask ){

String [] ipArr = ipAndMask. split ("/");

If (ipArr. length! = 2 ){

Throw new IllegalArgumentException ("invalid ipAndMask :"

+ IpAndMask );

}

Int netMask = Integer. valueOf (ipArr [1]. trim ());

If (netMask <0 | netMask> 31 ){

Throw new IllegalArgumentException ("invalid ipAndMask :"

+ IpAndMask );

}

Int ipInt = IPv4Util. ipToInt (ipArr [0]);

Int netIP = ipInt & (0 xFFFFFFFF <(32-netMask ));

Int hostScope = (0 xFFFFFFFF >>> netMask );

Return new int [] {netIP, netIP + hostScope };

}

Public static String [] getIPAddrScope (String ipAndMask ){

Int [] ipIntArr = IPv4Util. getIPIntScope (ipAndMask );

Return new String [] {IPv4Util. intToIp (ipIntArr [0]),

Listen 4util. intToIp (ipIntArr [0])};

}

Public static int [] getIPIntScope (String ipAddr, String mask ){

Int ipInt;

Int netMaskInt = 0, ipcount = 0;

Try {

IpInt = IPv4Util. ipToInt (ipAddr );

If (null = mask | "". equals (mask )){

Return new int [] {ipInt, ipInt };

}

NetMaskInt = IPv4Util. ipToInt (mask );

Ipcount = IPv4Util. ipToInt ("255.255.255.255")-netMaskInt;

Int netIP = ipInt & netMaskInt;

Int hostScope = netIP + ipcount;

Return new int [] {netIP, hostScope };

} Catch (Exception e ){

Throw new IllegalArgumentException ("invalid ip scope express ip :"

+ IpAddr + "mask:" + mask );

}

}

Public static String [] getIPStrScope (String ipAddr, String mask ){

Int [] ipIntArr = IPv4Util. getIPIntScope (ipAddr, mask );

Return new String [] {IPv4Util. intToIp (ipIntArr [0]),

Listen 4util. intToIp (ipIntArr [0])};

}

Public static void main (String [] args) throws Exception {

String ipAddr = "192.168.8.1 ";

Byte [] bytearr = IPv4Util. ipToBytesByInet (ipAddr );

StringBuffer byteStr = new StringBuffer ();

For (byte B: bytearr ){

If (byteStr. length () = 0 ){

ByteStr. append (B );

} Else {

ByteStr. append ("," + B );

}

}

System. out. println ("IP:" + ipAddr + "ByInet --> byte []: [" + byteStr

+ "]");

Bytearr = IPv4Util. ipToBytesByReg (ipAddr );

ByteStr = new StringBuffer ();

For (byte B: bytearr ){

If (byteStr. length () = 0 ){

ByteStr. append (B );

} Else {

ByteStr. append ("," + B );

}

}

System. out. println ("IP:" + ipAddr + "ByReg --> byte []: [" + byteStr

+ "]");

System. out. println ("byte []:" + byteStr + "--> IP :"

+ IPv4Util. bytesToIp (bytearr ));

Int ipInt = IPv4Util. ipToInt (ipAddr );

System. out. println ("IP:" + ipAddr + "--> int:" + ipInt );

System. out. println ("int:" + ipInt + "--> IP :"

+ IPv4Util. intToIp (ipInt ));

String ipAndMask = "192.168.1.1/24 ";

Int [] ipscope = IPv4Util. getIPIntScope (ipAndMask );

System. out. println (ipAndMask + "--> int address segment: [" + ipscope [0] + ","

+ Ipscope [1] + "]");

System. out. println (ipAndMask + "--> IP address segment :["

+ IPv4Util. intToIp (ipscope [0]) + ","

+ IPv4Util. intToIp (ipscope [1]) + "]");

String ipAddr1 = "192.168.1.1", ipMask1 = "255.255.255.0 ";

Int [] ipscope1 = IPv4Util. getIPIntScope (ipAddr1, ipMask1 );

System. out. println (ipAddr1 + "," + ipMask1 + "--> int address segment :["

+ Ipscope1 [0] + "," + ipscope1 [1] + "]");

System. out. println (ipAddr1 + "," + ipMask1 + "--> ip address segment :["

+ IPv4Util. intToIp (ipscope1 [0]) + ","

+ IPv4Util. intToIp (ipscope1 [1]) + "]");

}

}

I hope this article will help you with java programming.

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.