简体   繁体   中英

How to convert Binary String to int array?

Say I have an Integer, I convert it to binary string first.

            int symptomsM = 867;
            String symptomsBit = Integer.toBinaryString(symptomsM);

In this case, I would have symptomsBit as 1101100011 in binary.

But how can I further convert it to Int Array that has the same content, such as
symptomsBitArr[] = {1,1,0,1,1,0,0,0,1,1} ?

Okay. Here is what I have tried. I know symptomsBit.split(" ") isn't correct. But do not know how to further improve it.

            symptomsM = 867;
            String symptomsBit = Integer.toBinaryString(symptomsM);
            String[] symptomsBitArr = symptomsBit.split(" ");
            System.out.println("symptomsBit: " + symptomsBit);
            System.out.println("symptomsBitArray: " + symptomsBitArr);
            int[] symptomsArray = new int[symptomsBitArr.length];
            for (int i = 0; i < symptomsBitArr.length; i++) {
                 symptomsArray[i] = Integer.parseInt(symptomsBitArr[i]);
                 System.out.println("symptomsArray: " + symptomsArray);
                }

I tried the way Idos suggested as below:

            symptomsM = 867;
            String symptomsBit = Integer.toBinaryString(symptomsM);
            String[] symptomsBitArr = symptomsBit.split(" ");
            System.out.println("symptomsBit: " + symptomsBit);
            System.out.println("symptomsBitArray: " + symptomsBitArr);
            int[] symptomsArray = new int[symptomsBitArr.length];
            for (int i = 0; i < symptomsBitArr.length; i++) {
                 //symptomsArray[i] = Integer.parseInt(symptomsBitArr[i]);
                 symptomsArray[i] = Integer.parseInt(String.valueOf(symptomsBit.charAt(i)));
                 System.out.println("symptomsArray: " + symptomsArray);
                }

But it still not works. Here is the output:

symptomsBitArray: [Ljava.lang.String; @2a139a55
symptomsArray: [I@15db9742

I think this should do the trick:

String symptomBit = "1010";
for(int i = 0; i < symptomsBitArr.length; i++) {
    symptomsBitArr[i] = Integer.parseInt(String.valueOf(symptomsBit.charAt(i)));
}

// symptomsBitArr = [1,0,1,0]

// print array here
for (int j = 0; j < symptomsBitArr.length; j++) {
    System.out.println("symptomsArray: " + symptomsArray[j]);
}

I know symptomsBit.split(" ") isn't correct. But do not know how to further improve it.

Yes, you're almost there but must have made a few minor mistakes.

For one, that split() isn't correct because you're trying to split it on " " s but what you need is "" s because it's a continuous string.

That will give you the array that you think you're getting. But you also don't print the elements right. You should index into the array to get the element to print. Like so...

int symptomsM = 867;
String symptomsBit = Integer.toBinaryString(symptomsM);
String[] symptomsBitArr = symptomsBit.split("");
System.out.println("symptomsBit: " + symptomsBit);
System.out.println("symptomsBitArray: " + symptomsBitArr);
int[] symptomsArray = new int[symptomsBitArr.length];
for (int i = 0; i < symptomsBitArr.length; i++) {
    symptomsArray[i] = Integer.parseInt(symptomsBitArr[i]);
    System.out.println("symptomsArray: " + symptomsArray[i]);
} 

I am guessing you need each of value of the binary string to be of type int.

 String binString = Integer.toBinaryString(N);
 char[] intBinaryArray = binString.toCharArray();

Then while looping, you can easily do:

   for (int i = 0; i < binString.length() - 1; i++) {
            if (Character.getNumericValue(intBinaryArray[i]) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM