Mamado Mamado - 1 month ago 6x
Android Question

how to get hexadecimal value from combined booleans in Android Studio?

I want to get a hexadecimal value from four boolean variables something like this example:
boolean[] m = {true,false,true,true};

I want to get an string or Char that hold "B" which means 1011 in binary?


You can use below code to get your binary string, integer value and hexDecimal value.

    boolean[] m = {true,false,true,true};
    String binaryStr = "";
    for (boolean bit : m) {
        binaryStr = binaryStr + ((bit) ? "1" : "0" );
    int decimal = Integer.parseInt(binaryStr,2);
    String hexStr = Integer.toString(decimal,16);

In above code, binaryStr is your Binary String i.e. 1011 and its equivalent decimal and hexa decimal value are decimal and hexStr