Why isn't my Bit String to Hex String code printing the result?

Peanutcalota

I am trying to print the result of a bit string to hex string conversion but it prints nothing. In other words 000110011101 should print "19d" are my temp variables the issue?

Thanks in advance for any assistance, this is my code:

public static void BinaryToHex() {
        Scanner scanner = new Scanner(System.in);
        String bitString = "";
        String hexString = "";

    String[] hex = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C",
            "D", "E", "F" };
    String[] binary = { "0000", "0001", "0010", "0011", "0100", "0101", "0110",
            "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" };

    System.out.println("Enter a bit string: ");
    bitString = scanner.next();

    for (int i = 0; i < bitString.length(); i++) {
        char temp = bitString.charAt(i);
        String temp2 = "" + temp + "";
        for (int j = 0; j < binary.length; j++) {
            if (temp2.equalsIgnoreCase(binary[j])) {
                hexString = hexString + hex[j];
            }
        }
    }

    System.out.print("The equivalent hex string is ");
    System.out.println(hexString);
}
Sanket Makani
for (int i = 0; i < bitString.length(); i++) {
        char temp = bitString.charAt(i);
        String temp2 = "" + temp + "";
        for (int j = 0; j < binary.length; j++) {
            if (temp2.equalsIgnoreCase(binary[j])) {
                hexString = hexString + hex[j];
            }
        }
    }

In this loop you are trying to take one character of a bitString at a time and compare it with the input of binary[] array which has all input of length 4. So basically you are trying to comparing 1 length element with 4 length element which is never going to be true.

Therefore HexString will never get changed and it will print same as you have initialized it.So you are getting nothing while executing the code.

You can have a substring of bitString having 4 lengths and store it in temp2 then this code will work and for every 4 bits there is a HexCharacter so you can't make 1 bit number into binary number.

So as per your logic ,bitString length should be of multiples of 4 otherwise it won't give corresponding hex character from hex array.

Replace the for loop in your code with this.

if(bitString.length()%4!=0)
    System.out.println("Please Enter Valid Input.");
else
{
    for (int i = 0; i < bitString.length()/4; i++) 
    {
        String temp2 = bitString.substring(4*i,4*(i+1));
        for (int j = 0; j < binary.length; j++)
            {
            if (temp2.equalsIgnoreCase(binary[j]))
                {
                hexString = hexString + hex[j];
            }
        }
    }
    System.out.print("The equivalent hex string is ");
    System.out.println(hexString);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why isn't my code printing anything while I am attempting to reverse the words in a string

Using thunks: Why isn't my code printing sequentially?

Why isn't my function returning the string?

why my code get the result for average isn't seem correct?

Why isn't my code terminating within a loop when checking for 'exit' string?

Why my program is not printing string?

Why is my string not printing in a function?

Why isn't my str.replace() replacing this string?

Why isn't my Python regex matching part of the query string?

Why isn't my URL string being shortened as expected?

Why isn't my string being converted into an Array in jQuery

Why isn't my text file reading in Java Eclipse as a String?

Why is my second string replacement executing but the first isn't?

Why isn't string scanner not working in my other method?

Why isn't my alternate string replacement method working?

Why Isn't This L-System Code Returning a String?

Why is my string variable not printing a full string?

Why string to hex give me same result

Why isn't my variable printing in my GUI?

Why isn't my JavaScript printing to my HTML?

Hex to String with a bit of trick

Why isn't this ASCII art printing on multiple lines despite being a multiline string?

Why isn't my int numD counting upward and printing?

Why isn't this Javascript function printing into my HTML document?

Why isn't my program that converts decimal to binary printing correctly?

Why isn't my for-loop printing in the second loop?

Why isn't half of my cout statement printing?

Why isn't my char array pointer printing?

Why isn't my foreach loop printing the correct output?