-1
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

programmer
  • 13
  • 3
  • `int decimal = Integer.parseInt(c, 2);` `c` will be something like `"[1,0,1...]"` Perhaps you meant to join the list into a string first ("101...") – 001 Feb 02 '22 at 17:56
  • Find some solutions here: [Java: join array of primitives with separator](https://stackoverflow.com/a/47501361) Except you don't want a separator. Just an empty string (`""`). For example: `String c = IntStream.of(a).mapToObj(i -> String.valueOf(i)).collect(Collectors.joining());` – 001 Feb 02 '22 at 18:04
  • @JohnnyMopp , yeah that would be a nice idea, I can modify the string c to `String c = a.toString();` but its showing this `[I@372f7a8d` – programmer Feb 02 '22 at 18:08
  • You have to join the array into a string - see the link in my previous comment. _Or_, have `booleanToBinary()` return a string and not an array. For example using [`StringBuilder`](https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html) or just simple concatenation. – 001 Feb 02 '22 at 18:12

2 Answers2

0

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).

Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
0

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
WJS
  • 36,363
  • 4
  • 24
  • 39