Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hex string to int array conversion

Tags:

java

string

hex

I am stuck with these problem that how to convert a hex string to unsigned byte array or an Integer array..

Please look in this example.

String s= "1f5621ff8963545a12152250"; //this is my hex string

i want to convert this string to an int array

int buff[]={0x1f,0x56,0x21,0xff,0x89,0x63,0x54,0x5a,0x12,0x15,0x22,0x50};

how could i do that...

thanks for help..

like image 294
Shantanu Banerjee Avatar asked Apr 29 '26 16:04

Shantanu Banerjee


1 Answers

One easy way: compute the length of your int array by dividing the length of the String by 2. Loop over the string, calling substring() to create the two-character strings "1f", "56", etc. Pass each string to Integer.parseInt(string, 16) (the second argument means that the argument is a hex string.) Store the results in the array as you compute them.

like image 190
Ernest Friedman-Hill Avatar answered May 01 '26 06:05

Ernest Friedman-Hill