How can I convert a String to hex array to an int array using Java?

I have a class like below

public class EXOR{

    public static void conv(){
        String [] Parray={"243f6a88","85a308d3","13198a2e","03707344","a4093822","299f31d0","082efa98",
                "ec4e6c89","452821e6", "38d01377", "be5466cf","34e90c6c","c0ac29b7","c97c50dd","3f84d5b5","b5470917","9216d5d9","8979fb1b"};

        String binAddr[]=new String[18];
        for (int i=0;i<18;i++)
        {
            int x[]=new int[18];
            binAddr[i]= Integer.toBinaryString(Integer.parseInt(Parray[i],16));
            System.out.println("binernya : " +binAddr[i]);
        }

    }

    public static void main(String[] args){
        new EXOR().conv();

    }
}

      

and I want to convert this array to binary array format. I want to get the output as below. eg

00100100001111110110101010001000
10000111101000110000100011010011
................................

      

How to solve this problem?

+3


source to share


3 answers


I am assuming that you must have a numeric dot exception when executing your code. This happens when the hex string is out of the Integer range.

You can use:

    binAddr[i]= (new BigInteger(Parray[i],16)).toString(2);

      



instead

binAddr[i]= Integer.toBinaryString(Integer.parseInt(Parray[i],16));

This will help solve your problem for quick reference Large whole documentation

+1


source


Code:

public class EXOR {

    public static void conv(){

        String [] Parray={"243f6a88","85a308d3","13198a2e","03707344","a4093822","299f31d0","082efa98",
                "ec4e6c89","452821e6", "38d01377", "be5466cf","34e90c6c","c0ac29b7","c97c50dd","3f84d5b5","b5470917","9216d5d9","8979fb1b"};

        String [] binAddr = new String[Parray.length];

        for (int i = 0; i < binAddr.length; i++)
        {
            int strLen = Parray[i].length();
            binAddr[i] = "";

            for(int j = 0; j < strLen; j++) {
                String temp = Integer.toBinaryString(
                        Integer.parseInt(String.valueOf(
                                Parray[i].charAt(j)), 16));

                // Pad with leading zeroes
                for(int k = 0; k < (4 - temp.length()); k++) {
                    binAddr[i] += "0";
                }
                binAddr[i] += temp;
            }

            System.out.println("Original: " + Parray[i]);
            System.out.println("Binary: " + binAddr[i]);
        }
    }

    public static void main(String[] args){
        conv();
    }
}

      



First few lines of output:

Original: 243f6a88
Binary: 00100100001111110110101010001000
Original: 85a308d3
Binary: 10000101101000110000100011010011

      

0


source


We have Integer.MAX_VALUE = 2147483647 But the second element "85A308D3" = 2242054355. It is superior to the integer.

So you are using Integer.parseInt(85A308D3)

by calling java.lang.NumberFormatException

.

To fix this, change your code to use Long

insteadInteger

        binAddr[i] = Long.toBinaryString(Long.parseLong(Parray[i], 16));

      

Hope for this help!

0


source







All Articles