0

I am having trouble on this method where a hexadecimal number as a String is inputted with a preceding "0x" and outputted is the binary equivalent. For example "0x000A" should return "1010". I am having trouble with the variable ret. I try updating it in the forloop but when I return it, it still has the default value "". And if I don't use ret in the return statement I get a message saying "The value of the local variable binaryNumber is not used". I was wondering if I could get some clarification. Thanks a lot!

public static String hexToIntNBin(String hexInput) {
        
        // array of binary digits that correspond to hexadecmial
        String[] binaryArray = {"0000","0001","0010","0011",
                "0100","0101","0110","0111",
                "1000","1001","1010","1011",
                "1100","1101","1110","1111"};
        
        String binaryNumber = "";
        
        // loops backward through the input string after preceeding 0x
        // converts to binary
        for(int i=2; i<hexInput.length(); i++) {
            // Takes care of letter cases (10-16)
            if(hexInput.substring(i,i+1) == "A") {
                binaryNumber += binaryArray[10];
            }
            else if(hexInput.substring(i,i+1) == "B") {
                binaryNumber += binaryArray[11];
            }
            else if(hexInput.substring(i,i+1) == "C") {
                binaryNumber += binaryArray[12];
            }
            else if(hexInput.substring(i,i+1) == "D") {
                binaryNumber += binaryArray[13];
            }
            else if(hexInput.substring(i,i+1) == "E") {
                binaryNumber += binaryArray[14];
            }
            else if(hexInput.substring(i,i+1) == "F") {
                binaryNumber += binaryArray[15];
            }
            
            
        }
        return "Your number is ";
        
    }

0 Answers0