return fromHex(hex, -1);
}
public static byte[] fromHex(String hex, int expectedLength) {
if(hex == null) {
throw new ScriptusRuntimeException("hex string should not be null");
}
if (hex.length() % 2 == 1) {
RuntimeException e = new ScriptusRuntimeException("Hex.length="+hex.length());
e.printStackTrace();
throw e;
}
if(expectedLength != -1 && hex.length() / 2 != expectedLength) {
throw new ScriptusRuntimeException("Expected "+expectedLength+" bytes, got "+hex.length() / 2);
}
byte[] result = new byte[hex.length() / 2];
for (int i = 0; i != result.length; i++) {
result[i] = Integer.valueOf(hex.substring(i * 2, i * 2 + 2), 16).byteValue();
}