-3

I want to turn a string of binary to hexadecimal string

For example, I have a length of 24 binary string

"000100000010000000110000"

Convert hex becomes 0x10 0x20 0x30

How can I do?

I made reference to this: http: //stackoverflow.com/questions/25592084/converting-binary-string-to-a-hexadecimal-string-java

But I tried the results are not correct ...

I was wrong in what I ask?

     int digitNumber = 1;
        int sum = 0;
        String binary = "00000001";
        for(int i = 0; i < binary.length(); i++)
        {
            if(digitNumber == 1)
                sum+=Integer.parseInt(binary.charAt(i) + "")*8;
            else if(digitNumber == 2)
                sum+=Integer.parseInt(binary.charAt(i) + "")*4;
            else if(digitNumber == 3)
                sum+=Integer.parseInt(binary.charAt(i) + "")*2;
            else if(digitNumber == 4 || i < binary.length()+1)
            {
                sum+=Integer.parseInt(binary.charAt(i) + "")*1;
                digitNumber = 0;
                if(sum < 10)
                    System.out.print("0x"+sum);
                else if(sum == 10)
                    System.out.print("A");
                else if(sum == 11)
                    System.out.print("B");
                else if(sum == 12)
                    System.out.print("C");
                else if(sum == 13)
                    System.out.print("D");
                else if(sum == 14)
                    System.out.print("E");
                else if(sum == 15)
                    System.out.print("F");
                sum=0;
            }
            digitNumber++;  
        }
   }

The result is ...

0x00x1
3

2 Answers 2

0

Try this:

Integer.parseInt(binOutput, 2) instead of Integer.parseInt(binOutput)
Sign up to request clarification or add additional context in comments.

Comments

0

//Here is my Solution:

package stringprocessing;

/**
 *
 * @author zayeed
 */

public class StringProcessing {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        int index = 0;
        String bin = "0000000101100101011011100110011100000001000000000000000010101010010101100110010101100011011010010110110101100001001000000100111001100101011101000111011101101111011100100110101101110011001000000100100001000001010100110010000001001001010100110101001101010101010001010100010000100000010000010010000001010010010001010101000101010101010010010101001001000101010001000010000001010111010001010100010101001011010011000101100100100000010101000100010101010011010101000010000001000110010011110101001000100000010101000100100001000101001000000100011001001111010011000100110001001111010101110100100101001110010001110010000001000011010011110101010101001110010101000100100101000101010100110010111101000001010100100100010101000001010100110011101000100000010100000110100101101110011000010110110000101100001000000100000101011010001110110010000001000001010101000010000000000001111000000011000100110010001110100011000100110011001000000101000001001101001000000100111101001110";
        String[] hexString = new String[bin.length() / 4];
        for (int i = 0; i < bin.length() / 4; i++) {
            hexString[i] = "";
            for (int j = index; j < index + 4; j++) {
                hexString[i] += bin.charAt(j);
            }
            index += 4;
        }

        for (int i = 0; i < bin.length() / 4; i++) {
            System.out.print(hexString[i] + " ");
        }

       // System.out.println("\n" + bin.length());
        String[] result = binaryToHex(hexString);

        for (int i = 0; i < result.length; i++) {
            System.out.print("" + result[i].toUpperCase());
        }
        System.out.println("");
    }

    public static String[] binaryToHex(String[] bin) {
        String[] result = new String[bin.length];
        for (int i = 0; i < bin.length; i++) {
            result[i] = Integer.toHexString(Integer.parseInt(bin[i], 2));
        }
        //return Integer.toHexString(Integer.parseInt(bin[0], 2));
        return result;
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.