0

I have an int in a hex value, the problem now how I can get each byte of it.

int hex = 0xDF60;

What I want is:

int byte1 = 0xDF;
int byte2 = 0x60;

Already refer the answer from this question (Suggested by talex and andreas). But the result is not what I actually want:

byte[] bytes = ByteBuffer.allocate(4).putInt(0x9F16).array();

for (byte b : bytes) {
   System.out.format("0x%x ", b);
}

Output:

0x00 0x00 0x-97 0x22

Assumed the output like that because the value is already in hex.

Thank you.

4
  • 3
    int byte1 = hex >>> 8; int byte2 = (byte)hex; Commented Sep 27, 2017 at 2:51
  • @shmosel thanks, I'll try using that. Commented Sep 27, 2017 at 3:05
  • @shmosel Already tested it, the output is byte1: 223 byte2: 96 . Commented Sep 27, 2017 at 3:23
  • Yup, exactly like yours. Commented Sep 27, 2017 at 3:24

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.