I have a StringBuffer,strbuf=161656070200000000202020202001000000009E52;
.
I want to convert into a byte array that looks like this:
byte[] byte_array=new byte[]
{(byte)0x16,(byte)0x16,(byte)0x56,(byte)0x07,
(byte)0x02,(byte)0x00,(byte)0x00,(byte)0x00,
(byte)0x00,(byte)0x20,(byte)0x20,(byte)0x20,
(byte)0x20,(byte)0x20,(byte)0x01,(byte)0x00,
(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x9E,(byte)0x52};
Do I need to convert strbuf
to hex
string and then do getBytes()
?
What is the right approach to this problem?
You can try the following.
final String longHex = "161656070200000000202020202001000000009E52";
byte[] bytes = new BigInteger(longHex, 16).toByteArray();
System.out.println(Arrays.toString(bytes));
prints
[22, 22, 86, 7, 2, 0, 0, 0, 0, 32, 32, 32, 32, 32, 1, 0, 0, 0, 0, -98, 82]