Skip to content

Commit

Permalink
Simplify HEX-encoded byte string to byte conversion
Browse files Browse the repository at this point in the history
The code to convert a string containing a single byte encoded in HEX to
a byte is done in an unnecessary complex way. A parsing to int is
already performed and this integer value can directly be used as a byte
value instead of making an explicit conversion of an unsigned to a
signed value before parsing the byte again.
  • Loading branch information
HeikoKlare committed Apr 16, 2024
1 parent c119ba2 commit bcb197c
Showing 1 changed file with 1 addition and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -556,23 +556,7 @@ public static byte[] convertHexStringToByteArray(String str, int numBytes, int n
{
// convert string to byte
String oneByte = str.substring(i*2, i*2+2);

Integer number = Integer.valueOf(oneByte, 16);
if (number.compareTo(Integer.valueOf(Byte.toString(Byte.MAX_VALUE))) > 0)
{
int temp = number.intValue();
temp = temp - 256;

String tempStr = Integer.toString(temp);

Byte myByte = Byte.valueOf(tempStr);
bytes[i] = myByte.byteValue();
}
else
{
Byte myByte = Byte.valueOf(oneByte, 16);
bytes[i] = myByte.byteValue();
}
bytes[i] = (byte) Integer.parseInt(oneByte, 16);
}

return bytes;
Expand Down

0 comments on commit bcb197c

Please sign in to comment.