This is my code, it is so much easier to read then the solution provided and when I add random hex values in it does correctly convert to decimal. import java.util.Arrays; public class Solution { public static String[] hexces = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"}; public static String[] bintable = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; public static void main(String[] args) { String binaryNumber = "100111010000"; System.out.println("Binary number " + binaryNumber + " is equal to hexadecimal number " + toHex(binaryNumber)); String hexNumber = "8ef690"; System.out.println("Hexadecimal number " + hexNumber + " is equal to binary number " + toBinary(hexNumber)); } public static String toHex(String binaryNumber) { //write your code here boolean only1and0 = true; String binaryReturn = ""; /*for (int i = 0; i < binaryNumber.length(); i++) { if(binaryNumber.charAt(i) != '0' || binaryNumber.charAt(i) != '1') { only1and0 = false; } }*/ if (binaryNumber == null) { return binaryReturn; } if (binaryNumber.length() % 4 != 0) { for (int i = 0; i < binaryNumber.length() % 4; i++) { binaryNumber = '0' + binaryNumber.substring(0,binaryNumber.length()); } System.out.println(binaryNumber); } for (int i = 0; i < binaryNumber.length(); i = i+ 4) { String nibble = binaryNumber.substring(i,i+4); int binIndex = Arrays.asList(bintable).indexOf(nibble); binaryReturn = binaryReturn + hexces[binIndex]; } return binaryReturn; } public static String toBinary(String hexNumber) { //write your code here String hexReturn = ""; if (hexNumber == null) { return hexReturn; } for (int i = 0; i < hexNumber.length(); i++) { int hexIndex = Arrays.asList(hexces).indexOf(hexNumber.charAt(i) + ""); hexReturn = hexReturn + bintable[hexIndex]; } return hexReturn; } }