2010-06-19 5 views
2

저는 Java에서 새로운입니다. 나는 배우고있다.16 진수를 부울 변환하는 데 도움이 되셨습니까?

다음을 수행하려고합니다. 16 진수 문자열을 2 진수로 변환 한 다음 2 진수를 일련의 불린으로 처리합니다.

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 = leading0s - 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? 
    */ 
} 

조치를 수행하는 더 좋은 방법이 있어야한다고 생각합니다. 이것은 우아하지 않습니다. 또한, 어떻게 든 처리해야하는 이진 문자열 값이 붙어 있습니다.

답변

3
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

감사합니다. 나는 이것을 시험해보고 효과가 있는지 살펴볼 것입니다. –

+0

[참, 거짓, 참, 거짓, 참, 거짓, 참, 거짓] 고맙습니다! –

+0

루프를 단순화했습니다. –

관련 문제