0

Basically what I am needing is a way to take the input from the user of their ip address, then output the subsequent 8bits for each section as to which bits are on and off.

I currently have the user inputting their ip address in each section like so:

System.out.print("Please input the first set in your IP Address: ");
strHolder = kb.next();
first = Integer.parseInt(strHolder);

System.out.print("Please input the second set in your IP Address: ");
strHolder = kb.next();
second = Integer.parseInt(strHolder);

System.out.print("Please input the third set in your IP Address: ");
strHolder = kb.next();
third = Integer.parseInt(strHolder);

System.out.print("Please input the fourth set in your IP Address: ");
strHolder = kb.next();
fourth = Integer.parseInt(strHolder);

I'm not very experienced at Java. I've taken one class in college, and most of this I had to google how to do, but I've found nothing in regards to the bit process.

Simon
  • 113
  • 4
Ryan
  • 85
  • 1
  • 8
  • http://stackoverflow.com/questions/21856626/java-integer-to-binary-string (for how to make it look pretty) , http://stackoverflow.com/questions/2406432/converting-an-int-to-a-binary-string-representation-in-java?rq=1, http://stackoverflow.com/questions/23020633/method-for-string-input-to-binary-string, etc (for how to do the conversion) – user2864740 Jan 28 '15 at 19:27
  • `Integer.parseInt` ... lower case 'p' – Ascalonian Jan 28 '15 at 19:28
  • possible duplicate of [Converting an int to a binary string representation in Java?](http://stackoverflow.com/questions/2406432/converting-an-int-to-a-binary-string-representation-in-java) – user2864740 Jan 28 '15 at 19:29

3 Answers3

3
String s = Integer.toBinaryString(first);
bhspencer
  • 13,086
  • 5
  • 35
  • 44
  • I did this, but it won't put the complete bits unless the 128 bit is turned on. I tested it by doing 192.168.2.2 and it output: 11000000.10101000.10.10 Nevermind! I found out that I can do first_bit = String.format("%8s", Integer.toBinaryString(first)).replace(' ', '0'); Thank you for your help! – Ryan Jan 28 '15 at 21:09
0

If the input will be like: 123.124.125.126 (one string), you can make String[] strListHolder = strHolder.split(".") and operate on that.

Integer[] holder = new Integer[strListHolder.count];
for(int i = 0; i < strListHolder.count; i++){
    holder[i] = Integer.parseInt(strListHolder[i]);
}

Is that what you wanted? @EDIT After that you can check n'th bit of each partusing:

if ((holder[i] & (1 << n)) != 0)
{
   // The bit was set
}
Ascalonian
  • 14,409
  • 18
  • 71
  • 103
pcejrowski
  • 603
  • 5
  • 15
0

The IP address(IPV4) is of the format xxx.xxx.xxx.xxx.

Suppose the input is a string format. 1st seprate the 4 parts using split function in the string class. The you can take the individual parts and & them with 255 to get the bits which are set and use the same information to find the bits which are not set.

String[] str = ip.split(".");
for (String each : str) {
    System.out.println(Integer.parseInt(each) & 255);  // this would print the set bits just convert it to binary
}
Tom
  • 16,842
  • 17
  • 45
  • 54
Prasad Shinde
  • 652
  • 2
  • 8
  • 21