Java Hexadecimal to Binary
Java is a popular programming language that provides a wide range of tools to manipulate data in various formats. One of the essential features of Java is its ability to convert data between different formats, including hexadecimal and binary.
Hexadecimal Number:
Hexadecimal, commonly referred to as "hex," is a number system used in computing and digital electronics. It is a base-16 numbering system, meaning it uses 16 digits to represent numbers instead of the ten digits used in the decimal system.
The hexadecimal digits range from 0 to 9 and A to F, where A represents the decimal value of 10, B represents 11, and so on up to F representing 15.
The use of hexadecimal is prevalent in computing because it provides a compact and convenient way to represent binary data. Binary is a base-2 numbering system, meaning it uses only two digits, 0 and 1, to represent numbers. However, representing binary values in decimal notation can be cumbersome, as each bit in the binary number corresponds to a power of two.
Hexadecimal provides a more compact representation of binary values because each digit in a hexadecimal number corresponds to four bits in a binary number.
Conversion of Hexadecimal to Binary:
To convert the binary number to hexadecimal, we first group the binary digits into sets of four, starting from the rightmost digit. If necessary, we can add leading zeros to the leftmost group to make sure it has four digits. Then we can convert each group to its hexadecimal equivalent using the following table:
Binary | Hexadecimal |
0000 | 0 |
0001 | 1 |
0010 | 2 |
0011 | 3 |
0100 | 4 |
0101 | 5 |
0110 | 6 |
0111 | 7 |
1000 | 8 |
1001 | 9 |
1010 | A |
1011 | B |
1100 | C |
1101 | D |
1110 | E |
1111 | F |
Examples:
Example 1:
Input: hexadecimal number 0x3A
Output: binary number 0000 0000 0011 1010
Explanation:
We start by looking up the binary value for each hexadecimal digit using the conversion table provided in the explanation above. In this case, the first digit is 3, which has a binary value of 0011, and the second digit is A, which has a binary value of 1010.
Example 2:
Input: hexadecimal number 0xABF0
Output: binary number 1010 1011 1111 0000
Explanation:
Following the same procedure as before, we can convert each hexadecimal digit to its binary equivalent. The first digit is A, which has a binary value of 1010, the second digit is B, which has a binary value of 1011, the third digit is F, which has a binary value of 1111, and the fourth digit is 0, which has a binary value of 0000.
Implementation:
ALGORITHM:
Step 1: Initialize the hexadecimal number as input.
Step 2: Initialize an empty string to hold the binary representation of the hexadecimal number.
Step 3: Convert the hexadecimal number to binary by performing a sequence of modulo and division operations.
Step 4: Pad the binary number with leading zeros if necessary to ensure it has 16 bits.
Step 5: Return the binary representation of the hexadecimal number.
FileName: HexToBinaryConverter.java
import java.util.*;
public class HexToBinaryConverter {
public static void main(String[] args) {
// Convert hexadecimal number 0x3A to binary
int number1 = 0x3A; // Initialize number1 to hexadecimal value 0x3A
String binary1 = hexToBinary(number1); // Convert number1 to binary string
System.out.println("0x3A in binary: " + binary1); // Print the binary string
// Convert hexadecimal number 0xABF0 to binary
int number2 = 0xABF0; // Initialize number2 to hexadecimal value 0xABF0
String binary2 = hexToBinary(number2); // Convert number2 to binary string
System.out.println("0xABF0 in binary: " + binary2); // Print the binary string
}
public static String hexToBinary(int hex) {
// Initialize an empty string to hold the binary representation of the hexadecimal number
String binary = "";
// Convert the hexadecimal number to binary by performing a sequence of modulo and division operations
while (hex > 0) {
int remainder = hex % 2;
binary = remainder + binary;
hex = hex / 2;
}
// Pad the binary number with leading zeros if necessary to ensure it has 16 bits
while (binary.length() < 16) {
binary = "0" + binary;
}
// Return the binary representation of the hexadecimal number
return binary;
}
}
Output:
0x3A in binary: 0000000000111010
0xABF0 in binary: 1010101111110000
Complexity Analysis:
Time Complexity: The time complexity of this algorithm is O(log n), where n is the input hexadecimal number. The loop that performs the modulo and division operations runs log base 2 of n times, which is the number of times it takes to reduce the number to zero.
Space Complexity: The space complexity of this algorithm is O(log n), as the binary representation of the hexadecimal number is stored in a string variable. The size of the string is log base 2 of n, which is the number of digits in the binary representation.