Home > Back-end >  Having trouble converting a string into an integer
Having trouble converting a string into an integer

Time:02-03

public static int[] booleanToBinary(boolean[] b) {
    int[] arr = new int[b.length];
    for(int i = 0; i < b.length; i  ) {
        if(b[i] == true) {
             arr[i] = 1;
        }
        else{arr[i] = 0;};
        }
    
    return arr;
}



public static int binaryToInt(boolean[] b) {
    int[] a = booleanToBinary(b);
    String c = Arrays.toString(a);
    System.out.println(c);
    int decimal = Integer.parseInt(c, 2);
    
        System.out.println(decimal);
    

    
    return decimal;
    
}

 public static void main(String[] args) {
    boolean[] test = {false, true, false, true};
    System.out.println(Arrays.toString(booleanToBinary(test)));
    System.out.println(binaryToInt(test));
    
}

Blockquote I'm trying to turn the binary value into an Integer value, and I'm trying to do that using the binaryToInt method, and an NumberExceptionFormat is happening, I know that this error happens when java cannot convert a String into an Integer, can someone help me to fix this this error

CodePudding user response:

Upon converting the array of 0's and 1's to string using Arrays.toString, all the non-zeros and non-ones may be removed from the String using String::replaceAll:

public static int binaryToInt(boolean[] b) {
    int[] a = booleanToBinary(b);
    String c = Arrays.toString(a).replaceAll("[^01]", );
    System.out.println(c);
    int decimal = Integer.parseInt(c, 2);
    
    System.out.println(decimal);
    return decimal;    
}

Then the test:

 boolean[] test = {false, true, false, true};
System.out.println(Arrays.toString(booleanToBinary(test)));
System.out.println(binaryToInt(test));

prints:

[0, 1, 0, 1]
0101
5
5

However, here the order of bits is inversed as the arrays are printed starting from 0 to n (from lower bits).

CodePudding user response:

To convert a list of booleans to an int equivalent, try it like this. No need to use any strings or integer parsing methods.

// 100111 the value = 39
boolean[] b = {true, false, false, true, true, true};
int v = binaryToInt(b);
System.out.println(v);

prints

39
  • first multiply decimal by 2
  • then add 1 or 0 as appropriate.
  • continue, multiply and adding
public static int binaryToInt(boolean[] bools) {
    int decimal = 0;
    for (boolean b : bools) {
        decimal = decimal * 2   (b ? 1 : 0);
    }
    return decimal;
}

Similarly, to convert an array of booleans to an array of 1's or 0's.

public static int[] booleanToBinary(boolean[] bools) {
    int[] arr = new int[bools.length];
    int i = 0;
    for (boolean b : bools) {
        arr[i  ] = b ? 1 : 0;
    }
    return arr;
}

Note if you want to just convert your boolean array into a binary string, this will work.

public static String booleanToBinaryString(boolean[] bools) {
    int[] arr = new int[bools.length];
    String result = "";
    for (boolean b : bools) {
        result  = b ? 1 : 0;
    }
    return result;
}

System.out.println(booleanToBinaryString(b));

prints

100111
  •  Tags:  
  • Related