문제

int n= 0x234;

This will actually store 564 in n, but what if I want to have access and operate with 2,3 and 4?

도움이 되었습니까?

해결책

Hex digits are very easy to get: to access the value of the digit k, counting from right, shift the value right by 4*k, and mask with & 0x0F.

int n= 0x234;
int digit2 = (n >> 2*4) & 0x0F; // Gives you 2
int digit1 = (n >> 1*4) & 0x0F; // Gives you 3
int digit0 = (n >> 0*4) & 0x0F; // Gives you 4

다른 팁

How about this:

ByteBuffer bb = ByteBuffer.allocate(4);
bb.putInt(0x234);
for (byte b : bb.array()) {
    System.out.print(String.format("%02X ", b) + " "); //00 00 02 34
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top