Java integer to binarystring method
Thе Intеgеr.toBinaryString() mеthod in Java is a fundamеntal utility for convеrting an intеgеr valuе into its binary rеprеsеntation as a string. Thе mеthod is a part of thе Java standard library and providеs a simplе and еfficiеnt way to pеrform this convеrsion. It is particularly usеful in scеnarios whеrе you nееd to work with binary data or manipulatе binary rеprеsеntations of numbеrs.
Method Declaration:
public static String toBinaryString(int i)
Parameters:
i: The integer value that you want to convert to a binary string.
Return Value:
The method returns a String that represents the binary (base 2) equivalent of the input integer.
Exception:
The Integer.toBinaryString(int i) method in Java does not explicitly declare any exceptions that it can throw. Therefore, it does not throw checked exceptions.
Examples:
Input: 10
Output: Binary representation of 10 is 1010
Input: 9
Output: Binary representation of 9 is 1001
Input: 21
Output: Binary representation of 21 is 10101
Input: 12
Output: Binary representation of 12 is 1100
Input: 15
Output: Binary representation of 15 is 1111
Example 1:
Implementation:
FileName: BinaryConverter1.java
import java.util.*; public class BinaryConverter1 { public static void main(String[] args) { int number = 21; String binaryRepresentation = Integer.toBinaryString(number);//conversion System.out.println("Binary representation of " + number + " is " + binaryRepresentation); } }
Output:
Binary representation of 21 is 10101
Explanation: It is the most straightforward method. We use the Integer.toBinaryString() method, as previously explained, to convert the integer to a binary string.
Example 2:
FileName: BinaryConverter2.java
import java.util.Scanner; public class BinaryConverter2 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter an integer: "); int number = scanner.nextInt(); // Check if the input number is negative if (number < 0) { System.out.println("Please enter a non-negative integer."); } else { String binaryRepresentation = Integer.toBinaryString(number); System.out.println("Binary representation of " + number + " is " + binaryRepresentation); } scanner.close(); } }
Output:
Enter an integer: 12 Binary representation of 12 is 1100
Explanation: Thе codе takеs usеr input, chеcks if it's non-nеgativе, and thеn converts it to its binary rеprеsеntation using Intеgеr.toBinaryString(int i) mеthod bеforе displaying thе rеsult.
Example 3:
Implementation:
FileName: BinaryConverter3.java
import java.util.Scanner; public class BinaryConverter3 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Prompt the user to enter an integer System.out.print("Enter an integer: "); // Read the integer input int number = scanner.nextInt(); // Initialize an empty string to store the binary representation String binaryRepresentation = ""; // Create a copy of the input number to display later int originalNumber = number; // Calculate the binary representation of the input number while (number > 0) { int bit = number % 2; binaryRepresentation = bit + binaryRepresentation; number /= 2; } // Display the binary representation System.out.println("Binary representation of " + originalNumber + " is " + binaryRepresentation); // Close the scanner scanner.close(); } }
Output:
Enter an integer: 9 Binary representation of 9 is 1001
Example 4:
Implementation:
FileName: BinaryConverter4.java
import java.util.*; public class BinaryConverter4 { public static void main(String[] args) { // Array of integers to be converted to binary int[] numbers = {0, 1, 2, 5, 10, 15, 25, 50, 100, 255}; // Loop through each number in the array for (int number : numbers) { // Convert the number to its binary representation String binaryRepresentation = convertToBinary(number); // Display the binary representation along with the original number System.out.println("Binary representation of " + number + " is " + binaryRepresentation); } } // Recursive method to convert an integer to binary public static String convertToBinary(int number) { // Base case: If the number is 0, return "0" if (number == 0) { return "0"; } // Base case: If the number is 1, return "1" if (number == 1) { return "1"; } // Recursive case: Convert the number to binary recursively return convertToBinary(number / 2) + (number % 2); } }
Output:
Binary representation of 0 is 0 Binary representation of 1 is 1 Binary representation of 2 is 10 Binary representation of 5 is 101 Binary representation of 10 is 1010 Binary representation of 15 is 1111 Binary representation of 25 is 11001 Binary representation of 50 is 110010 Binary representation of 100 is 1100100 Binary representation of 255 is 11111111
Example 5:
Implementation:
FileName: BinaryConverter5.java
import java.util.*; public class BinaryConverter5 { public static void main(String[] args) { int number = 15; String binaryRepresentation = String.format("%8s", Integer.toBinaryString(number)).replace(' ', '0'); System.out.println("Binary representation of " + number + " is " + binaryRepresentation); } }
Output:
Binary representation of 15 is 00001111
Explanation: In this implementation, we use String.format() to pad the binary string with leading zeros to a fixed length. This can be useful for maintaining a consistent width for binary representations.