Parse and Format a Number into Binary in Java



In this article, we will learn to parse and format a number into binary using Java. Binary representation plays a crucial role, especially when working with low-level operations, data storage, or network communication. Java, a widely used object-oriented programming language, provides several tools to manipulate numbers and convert them into different formats.

What is Binary Representation?

A binary number is a base-2 numeral system that uses two digits: 0 and 1. Every number in the Decimal System (base-10) can be converted into binary by representing it as a sum of powers of 2.
For example ?

  • The decimal number 10 in binary is 1010.
  • The decimal number 5 in binary is 101.

Converting an Integer to a Binary

To parse and format a number into binary, Java offers built-in methods in the Integer and Long classes, depending on whether you are working with integers or long integers.

  • The Integer.toBinaryString() method in Java converts an integer into its binary (base-2) string representation. It is commonly used for binary operations and understanding the bitwise representation of numbers.

Using the Integer.parseInt() method ?

val = Integer.parseInt("11111111", 2);

Using the Integer.toString() method will format the above value into binary ?

String str = Integer.toString(val, 2);

Example

Below is an example of parsing and formatting a number into binary in Java ?

public class Demo {
   public static void main(String []args){
      int val = 566;
      val = Integer.parseInt("11111111", 2);
      System.out.println(val);
      String str = Integer.toString(val, 2);
      System.out.println(str);
   }
}

Output

255
11111111

Converting a Long to Binary

For long integers, Java simplifies this process using the Long.toBinaryString() method, which converts a long value into its binary string equivalent.

  • Long.toBinaryString(long value): This static method converts the specified long value into its binary representation as a String. The returned string represents the number in a base-2 format without any leading zeros.

Example

Below is an example of parsing and formatting a Long number into binary in Java ?

public class BinaryConversion {
    public static void main(String[] args) {
        long number = 12345L;
        String binaryString = Long.toBinaryString(number);
        System.out.println("Binary representation of " + number + " is: " + binaryString);
    }
}

Output

Binary representation of 12345 is: 11000000111001

Parsing a Binary String Back to Decimal

Converting a binary string back to its decimal form is equally essential, especially when dealing with binary data in practical scenarios. Java provides methods like Integer.parseInt() and Long.parseLong() to interpret binary strings and convert them into their numeric equivalents.

  • Integer.parseInt(String s, int radix): This method parses the string s as an integer in the specified radix (base). When the radix is set to 2, the method interprets the string as a binary number and returns its decimal value.

Example

Below is an example of parsing a binary string back to decimal in Java ?

public class BinaryConversion {
    public static void main(String[] args) {
        String binaryString = "1010";
        int decimal = Integer.parseInt(binaryString, 2);
        System.out.println("Decimal representation of " + binaryString + " is: " + decimal);
    }
}

Output

Decimal representation of 1010 is: 10
Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2024-12-26T20:42:49+05:30

598 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements