如何从组合布尔中得到十六进制值?

时间:2016-08-26 13:25:42

标签: java

我想从四个布尔变量中获取十六进制值,如下例所示:

boolean[] m = {true, false, true, true};

我想获得一个包含B的字符串或字符,这意味着二进制文件中的1011

PS:我正在开发Android应用程序。

5 个答案:

答案 0 :(得分:2)

您可以使用以下代码获取二进制字符串,整数值和十六进制值。

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

在上面的代码中,binaryStr是您的二进制字符串,即1011,其等效的十进制和十六进制十进制值为decimalhexStr

答案 1 :(得分:1)

boolean[] booleanArray = new boolean[] { true, false, false, true };
String binaryString = Arrays.toString(booleanArray).replace("true", "1").replace("false", "0");

并将该binaryString转换为十六进制值

答案 2 :(得分:1)

尽管上述答案确实有效,但它们有其局限性。由于32位整数限制,第一个限制是32个布尔值的上限。但是上面的代码还使用了内置方法,它们不允许初学者掌握实际发生的情况。

如果您只想完成工作,请使用上面的内容。可能会更有效率。我之所以发布此解决方案,是因为我认为它可以使想要掌握实际概念的人做得更好。

(希望注释有一点道理)

public static String booleanArrayToHex1(boolean[] arr){

    if(!(arr.length%4==0)){arr=makeMultOf4(arr);} //If you are using arrays not in multiples of four, you can put a method here to add the correct number of zeros to he front.
    //Remove the above line if your array will always have a length that is a multiple of 4

    byte[] hexValues=new byte[arr.length/4]; 
    //The array above is where the decimal hex values are stored, it is byte because the range we need is 0-16, bytes are the closest to that with the range of 0-255

    int hexCounter=arr.length/4-1; //counts what hex number you are calculating

    /* The below loop calculates chunks of 4 bianary numbers to it's hex value
     * this works because 16 is a power of 2
     * so if we simpily squish those numbers together it will be the same as finding the accual integer value of the whole thing
     * This can go to higher numbers because we do not have the 32 bit integer limit
     * it runs in reverse because the lowest value is on the right of the array (the ones place is on the right)
     */
    for(int i=arr.length-1;i>=0;i--){
        if(arr[i]){
            int place=1; //will count the value of a bianary number in terms of its place

            for(int j=3;j>i%4;j--) 
            //loop multiplies the bianary number by 2 j times, j being what place it's in (two's, four's, eight's). This gives the correct value for the number within the chunk.
            //This is why the array needs to be a multiple of 4
                place*=2;

            hexValues[hexCounter]+=place; //this will add that place value to the current chunk, only if this place in the boolean array is true (because of the above if - statement)
        }

        if(i%4==0)//moves the chunk over one every 4 binary values
            hexCounter--;
    }

    //Everything below simpily takes the array of decimal hex values, and converts to a alpha-numeric string (array to normal hex numbers)
    String ret="";
    for(byte b:hexValues)
        switch(b){
            case 10:
            ret=ret+"A";
            break;
            case 11:
            ret=ret+"B";
            break;
            case 12:
            ret=ret+"C";
            break;
            case 13:
            ret=ret+"D";
            break;
            case 14:
            ret=ret+"E";
            break;
            case 15:
            ret=ret+"F";
            break;
            default:
            ret=ret+b;
            break;
        }
    return ret;
}

如果您的数组长度不是4的倍数,则需要使数组成为4的倍数来完成此工作。下面有一些代码可以做到这一点。它没有被评论,并且可能没有那么有效,但是它有效。

public static boolean[] makeMultOf4(boolean[] arr){
    if(arr.length%4==0){return arr;}
    boolean[] array=new boolean[arr.length+(arr.length%4==1?3:arr.length%4==2?2:1)];
    for(int i=0;i<array.length;i++){
        try{
            array[i]=arr[i-(arr.length%4==1?3:arr.length%4==2?2:1)];
        }catch(Exception e){
            array[i]=false;
        }
    }
    return array;
}

答案 3 :(得分:0)

您可以使用此逻辑:

String x = ""; 
for(int i  = 0 ; i < m.length ; i++){
    if(m[i])
      x += "1";
    else
       x += "0";
 }

Log.i("values = ",x);

答案 4 :(得分:0)

对于每个布尔值,如果binary 0,则附加到true字符串1,否则使用Integer.parseInt()将二进制字符串转换为整数实例,最后使用{{1}将整数转换为十六进制字符串方法

Integer.toHexString()