简体   繁体   中英

Convert binary String[] to int[] java

If I want to convert binary String[] to binary int[], in which part I made a mistake, because I got wrong result?

I have binary string array like this: {"010101","000011","111100"} I want to convert it to binary integer array with the same result: {"010101","000011","111100"}

I tried but the result is not as my expectation. Here is my code:

public class StringArr2IntArr {
static int[] sAtoiA;
public static void main(String[] args) {
    String[] str=new String[]{"010101","000011","111100"};
    System.out.println("String arr: ");
    for (String str1 : str) {
        System.out.println("strArr= "+str1);
    }

    sAtoiA = convertStringArraytoIntArray(str);
    System.out.println("sA to iA:");
    for (int iA : sAtoiA) {
        System.out.println("iArr= "+iA);
    }
}

private static int[] convertStringArraytoIntArray(String[] sArr) {
    if (sArr!=null) {
        int[] iArr=new int[sArr.length];
        for (int i = 0; i < sArr.length; i++) {
            iArr[i]=Integer.parseInt(sArr[i]);
        }
        return iArr;
    }
    return null;
  }
}

Because I got result:

String arr: 
strArr= 010101
strArr= 000011
strArr= 111100
sA to iA:
iArr= 10101
iArr= 11
iArr= 111100

The results should be:

String arr: 
strArr= 010101
strArr= 000011
strArr= 111100
sA to iA:
iArr= 010101
iArr= 000011
iArr= 111100

The point is the result should be same before and after converter. Is there somebody can help me?

With some tweaks, this produces the output as you described:

public static void main(String[] args) {
    String[] str = new String[]{"010101", "000011", "111100"};
    System.out.println("String arr: ");
    for (String str1 : str) {
        System.out.println("strArr= " + str1);
    }

    sAtoiA = convertStringArraytoIntArray(str);
    System.out.println("sA to iA:");
    for (int iA : sAtoiA) {
        System.out.println("iArr= " + String.format("%6s", Integer.toString(iA, 2)).replaceAll(" ", "0"));
    }
}

private static int[] convertStringArraytoIntArray(String[] sArr) {
    if (sArr != null) {
        int[] iArr = new int[sArr.length];
        for (int i = 0; i < sArr.length; i++) {
            iArr[i] = Integer.parseInt(sArr[i], 2);
        }
        return iArr;
    }
    return null;
}

The problem is that you convert a value in an integer, which is a number and does not support leading 0's. So, it cuts them off. If you want to use this sequence of number I would recommend to keep working with your strings and extract the digits when needed.

The method Integer.parseInt parses number by default in base10. You also have a method where you can specify the base the number is in: Integer.parseInt(theString, 2) . When you do it this way however, converting it back requires additional work, since integers are represented by default in base10 when you print them out. So to convert them back you need the method Integer.toBinaryString(theInteger) . The method does get rid of leading zeros so the representation does not stay the same. You can fix this by formatting the binary string:

int length = 8;
String result = String.format("%" + length + "s").replaceAll(" ", "0");

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