Link to home
Start Free TrialLog in
Avatar of xenia27
xenia27Flag for Taiwan, Province of China

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
ASKER CERTIFIED SOLUTION
Avatar of x4u
x4u

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of xenia27

ASKER

OK...thanks~  This works exactly what I want!!  ^^
Avatar of x4u
x4u

You are welcome. ;-)
Avatar of CEHJ
byte[] bytes = new BigInteger("1EA23", 16).toByteArray();
> byte[] bytes = new BigInteger("1EA23", 16).toByteArray();
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.
OK - you've rumbled me - it can't be completely done in one line only ;-)