xenia27
asked on
Convert a hex string to byte array
Hi,
I have this hex string, "1EA23", I would like to put this hex string into a byte array...
For example...
String hexString = "1EA23";
byte byteArray[] = new byte[4];
the value of byteArray should be...
byteArray[0] = (byte)0;
byteArray[1] = (byte)0x01;
byteArray[2] = (byte)0xEA;
byteArray[3] = (byte)0x23;
How can I do this?? I try to do it with substring...but it seems not work well...help please~~~~
Xenia
I have this hex string, "1EA23", I would like to put this hex string into a byte array...
For example...
String hexString = "1EA23";
byte byteArray[] = new byte[4];
the value of byteArray should be...
byteArray[0] = (byte)0;
byteArray[1] = (byte)0x01;
byteArray[2] = (byte)0xEA;
byteArray[3] = (byte)0x23;
How can I do this?? I try to do it with substring...but it seems not work well...help please~~~~
Xenia
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You are welcome. ;-)
byte[] bytes = new BigInteger("1EA23", 16).toByteArray();
> byte[] bytes = new BigInteger("1EA23", 16).toByteArray();
This would an array that is only 3 in length.
This would an array that is only 3 in length.
I'm not really awake yet. ;-)
This would create an array that is only 3 bytes in length.
This would create an array that is only 3 bytes in length.
OK - you've rumbled me - it can't be completely done in one line only ;-)
ASKER