Converting an IP address to binary values (Java)

15,412

Solution 1

Building on jtahlborn' answer:

byte[] bytes = InetAddress.getByName(data_in).getAddress();
data_out = new BigInteger(1, bytes).toString(2);

Now data_out contains the IP address as a binary number.

Solution 2

you can use InetAddress to parse the textual representation and convert to a byte[]. you can use BigDecimal to convert a byte[] to a big integer.

Solution 3

Scanner in = new Scanner(System.in);
System.out.println("Please enter an IP Address.");
String ip = in.nextLine();
String[] octetArray = ip.split("\\.");
for (String string : octetArray){
    int octet = Integer.parseInt(string);
    String binaryOctet = Integer.toBinaryString(octet);
    System.out.println(binaryOctet);
}

so for input 10.10.150.1 output will be

1010

1010

10010110

1

Share:
15,412
jerms246
Author by

jerms246

I.T. Professional Mostly networks and hardware

Updated on July 16, 2022

Comments

  • jerms246
    jerms246 almost 2 years

    I'm trying to write a program in Java that will take an IP address and convert to binary.

    Here is what I have so far:

    import java.util.Scanner;
    
    public class IpConverter{
    
    public static void main (String[]args)
    {
    
        int result;
    
        String data_in;
    
        int data_out;
    
            Scanner scan = new Scanner(System.in);
    
            try
            {
                System.out.print("Enter an IP address: ");
                data_in = scan.next();
    
                data_out = Integer.parseInt(data_in, 10);
                System.out.println (data_in + "is equivalent to" + data_out);
            }
            catch (NumberFormatException nfe){
                System.out.println("Wrong data type!");
    
            }
        }
    }
    
    • Marc B
      Marc B about 12 years
      Assuming your ips are being entered in dotted.quad format (eg 127.0.0.1), parseint isn't going to give you a full 32bit representation of that, since that's not a valid int.
    • Jeffrey
      Jeffrey about 12 years
      For future reference, if you select all of your code and press CTRL+K you will get one big code block instead of the many small ones you originally had.
    • jerms246
      jerms246 about 12 years
      Yes, 127.0.0.1 would be the format I want.
    • Shuo Ran
      Shuo Ran about 12 years
      There are answers to the same question at stackoverflow.com/questions/1146581/…
  • jerms246
    jerms246 about 12 years
    The string ip = in.nextLine(); command is giving me an error. "error: cannot find symbol String ip = in.nextLine();"
  • Dan675
    Dan675 about 12 years
    That's weird, the code is copied directly from my code that ran fine.
  • jerms246
    jerms246 about 12 years
    Anything else i need to import?Other than java.util.Scanner? I also have java.lang.String imported as well.
  • Dan675
    Dan675 about 12 years
    Scanner is the only think you should need imported.
  • jerms246
    jerms246 about 12 years
    Not working, man. You think you could add some more of your code to see if I'm missing anything?