BRDroid
BRDroid

Reputation: 4388

Getting individual bytes from hexadecimal value

Is there is a way I can get individual Bytes from a hexa decimal value in java

If I have a hexadecimal value 0x190(400), I want to get 0x90 and 0x01 If I have a hexadecimal value 0x89(137), I want to get 0x89 and 0x00

I am new to these and unable to find a way to get them individually.

Thank you for your help in advance

Thanks R

Upvotes: 2

Views: 364

Answers (2)

jpact
jpact

Reputation: 1072

If your number is represented as the integral value, you can use bit mask to isolate particular byte.

int value = 0x190;

byte byteValue = (byte) ((value >>> i*8) & 0xff);
String byteAsString = String.format("0x%02x", byteValue);

where i represents i-th byte (starting at 0)

Upvotes: 3

Andrey Rosa
Andrey Rosa

Reputation: 91

If is string value

like this:

let hexDecimal = "0x89(137)";
let splitValue = hexDecimal .split("(");
let response =  splitValue[0]; `

Upvotes: 0

Related Questions