Convert hex String To Byte Array in Java
Description
The following code shows how to convert hex String To Byte Array.
Example
/* w ww . j ava 2 s .c o m*/
import java.util.Arrays;
public class Main {
public static void main(String[] argv){
System.out.println(Arrays.toString(hexStringToByteArray("ffff")));
}
public static byte[] hexStringToByteArray(String s) {
byte[] b = new byte[s.length() / 2];
for (int i = 0; i < b.length; i++) {
int index = i * 2;
int v = Integer.parseInt(s.substring(index, index + 2), 16);
b[i] = (byte) v;
}
return b;
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »