What's wrong with my code? The output seems correct but the task is not completed. Can someone help me, please?
package en.codegym.task.pro.task09.task0908;
/*
Binary to hexadecimal converter
*/
public class Solution {
private static final String HEX = "0123456789abcdef";
public static void main(String[] args) {
String binaryNumber = "100111010000";
System.out.println("Binary number " + binaryNumber + " is equal to hexadecimal number " + toHex(binaryNumber));
String hexNumber = "9d0";
System.out.println("Hexadecimal number " + hexNumber + " is equal to binary number " + toBinary(hexNumber));
}
public static String toHex(String binaryNumber) {
if (binaryNumber == null)
return "";
String h = "";
int len = binaryNumber.length();
while (binaryNumber.length() % 4 != 0) {
binaryNumber = "0" + binaryNumber;
}
for (int i = 0; i < len; i += 4) {
int index0 = binaryNumber.charAt(len - i - 1) == '0' ? 0 : 1;
int index1 = binaryNumber.charAt(len - i - 1 - 1) == '0' ? 0 : 1;
int index2 = binaryNumber.charAt(len - i - 2 - 1) == '0' ? 0 : 1;
int index3 = binaryNumber.charAt(len - i - 3 - 1) == '0' ? 0 : 1;
int index = index0 + index1 * 2 + index2 * 4 + index3 * 8;
h = HEX.charAt(index) + h;
}
return h;
}
public static String toBinary(String hexNumber) {
if (hexNumber == null)
return "";
int len = hexNumber.length();
String b = "";
for (int i = 0; i < len; i++) {
int decimalNumber = HEX.indexOf(hexNumber.charAt(i));
String aux = "";
if (decimalNumber == 0) {
aux = "0000";
}
while (decimalNumber != 0) {
aux = decimalNumber % 2 + aux;
decimalNumber = decimalNumber / 2;
}
b += aux;
}
return b;
}
}