Como no había respuesta para cadena hexadecimal de un solo byte conversión, aquí es mío: uso
private static byte hexStringToByte(String data) {
return (byte) ((Character.digit(data.charAt(0), 16) << 4)
| Character.digit(data.charAt(1), 16));
}
muestra:
hexStringToByte("aa"); // 170
hexStringToByte("ff"); // 255
hexStringToByte("10"); // 16
O también puede probar el Integer.parseInt(String number, int radix)
imo, es mucho mejor que otros.
// first parameter is a number represented in string
// second is the radix or the base number system to be use
Integer.parseInt("de", 16); // 222
Integer.parseInt("ad", 16); // 173
Integer.parseInt("c9", 16); // 201
Ver http://stackoverflow.com/questions/140131/convert-a-string-representation-of-a-hex-dump-to-a-byte -array-using-java – rafraf
'byte n [] = str.getBytes()' hará la tarea. Sin embargo, obtendrá un error en 'new' ya que es una palabra clave y no puede usarse como identificador. – backslashN