开发者

Help with converting Hex to booleans?

I am new at Java. I am learning.

I am trying to do the following: convert a hexadecimal string into a binary, then process the binary into a series of booleans.

    public static void getStatus() {
    /*
     * CHECKTOKEN is a 4 bit hexadecimal 
     * String Value in FF format. 
     * It needs to go into binary format 
     */
    //LINETOKEN.nextToken = 55 so CHECKTOKEN = 55
    CHECKTOKEN = LINETOKEN.nextToken();
    //convert to Integer  (lose any leading 0s)
    int binaryToken = Integer.parseInt(CHECKTOKEN,16);
    //convert to binary string so 55 becomes 85 becomes 1010101
    //should be 01010101
    String binaryValue = Integer.toBinaryString(binaryToken);
    //Calculate the number of required leading 0's
    int leading0s = 8 - binaryValue.length();
    //add leading 0s as needed
    while (leading0s != 0) {
        binaryValue = "0" + binaryValue;
        leading0s = l开发者_如何学Ceading0s - 1;
    }
    //so now I have a properly formatted hex to binary
    //binaryValue = 01010101
    System.out.println("Indicator" + binaryValue);
    /*
     * how to get the value of the least 
     * signigicant digit into a boolean 
     * variable... and the next?
     */
}

I think there must be a better way to perform the action. This is not elegant. Also, I'm stuck with a binary string value which needs to be processed somehow.


public static void main(String[] args) {

    String hexString = "55";

    int value = Integer.parseInt(hexString, 16);

    int numOfBits = 8;

    boolean[] booleans = new boolean[numOfBits];

    for (int i = 0; i < numOfBits; i++) {
        booleans[i] = (value & 1 << i) != 0;
    }

    System.out.println(Arrays.toString(booleans));
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜