Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the conversion from IP address to binary by Java

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "Java how to achieve IP address to binary conversion". In daily operation, I believe many people have doubts about how to achieve IP address to binary conversion in Java. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for everyone to answer the question of "how to achieve IP address to binary conversion in Java". Next, please follow the editor to study!

Java programming realizes the conversion from decimal IP address to binary IP address.

For example, 192.168.1.100, after conversion: 11000000.10101000.00000001.01100100

Request:

1. Define a custom exception class InvalidIP (check type), which means that the IP address is illegal-- for example, the number of dotted decimal IP segments is not 4; the values of each segment are not in the range of 0,255

two。 Define the public static method convertIP to implement the conversion. The dotted decimal IP address is the parameter, and the converted binary IP is the return value. Check the validity of the parameter IP address in the method. If illegal, please throw a custom exception InvalidIP.

Public static String convertIP (String ip) throws InvalidIP

Tip:

(1) split the string first, "." Is a delimiter and is stored in String []

(2) convert the string to int, consider the binary problem, and generate int []

(3) if the length of the array is not 4, IP throws an exception illegally; if the converted value is not in the range of 0,255, IP throws an exception illegally.

3. Implement user input, method invocation and output in the main method, that is:

(1) read the keyboard input through the nextLine () of the Scanner class, or input the dialog box text through JOptionPane.showInputDialog (String str) to get the dotted decimal IP string str to be converted

(2) call convertIP (str) to implement the conversion and get the returned value. Exception handling is required here.

(3) output result, command line or dialog box JOptionPane.showMessageDialog (String str)

Import com.sun.deploy.util.StringUtils;import java.util.Arrays;import java.util.Scanner; class InvalidIP extends Exception {/ / defines a custom exception class InvalidIP (check type), which means that the IP address is illegal-for example, the number of dotted decimal IP segments is not 4 The values of each segment are not in the range of 0,255 InvalidIP (String s) {super (s); return }} public class Test6_1 {public static String convertIP (String str) throws InvalidIP {/ / defines the public static method convertIP to implement the conversion. The dotted decimal IP address is the parameter, and the translated binary IP is the return value. Check the validity of the parameter IP address within the method. If illegal, please throw a custom exception InvalidIP String [] ipstr = str.split ("\\."); String return_ip = null If (ipstr.length==4) {int [] array = Arrays.stream (ipstr) .mapToInt (Integer::parseInt). ToArray (); String [] ipa = new String [4]; for (int iTuno

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report