diff options
author | Sven Gothel <[email protected]> | 2022-01-27 07:42:49 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2022-01-27 07:42:49 +0100 |
commit | d4d2dbe074814505f96b9e03fbcbfc1db4d18a13 (patch) | |
tree | 5e99c734845f46c5e6dc6964024eb2c017ec6ea9 /java_base | |
parent | 25037b693981e0541f818b9aa44e1c20c507e284 (diff) |
Add Java/C++ hexStringBytes(..); Fix Java's bytesHexString(..) path !lsbFirst; Add unit tests
Diffstat (limited to 'java_base')
-rw-r--r-- | java_base/org/jau/util/BasicTypes.java | 59 |
1 files changed, 56 insertions, 3 deletions
diff --git a/java_base/org/jau/util/BasicTypes.java b/java_base/org/jau/util/BasicTypes.java index bb0c404..ca90428 100644 --- a/java_base/org/jau/util/BasicTypes.java +++ b/java_base/org/jau/util/BasicTypes.java @@ -40,6 +40,58 @@ public class BasicTypes { } /** + * Converts a given hexadecimal string representation to a byte array. + * + * In case a non valid hexadecimal digit appears in the given string, + * conversion ends and returns the byte array up until the violation. + * + * @param hexstr the hexadecimal string representation + * @param lsbFirst low significant byte first + * @param checkLeading0x if true, checks for a leading `0x` and removes it, otherwise not. + * @return the matching byte array + */ + public static byte[] hexStringBytes(final String hexstr, final boolean lsbFirst, final boolean checkLeading0x) { + final int offset; + if( checkLeading0x && hexstr.startsWith("0x") ) { + offset = 2; + } else { + offset = 0; + } + final int size = ( hexstr.length() - offset ) / 2; + final byte[] b = new byte[size]; + if( lsbFirst ) { + for (int i = 0; i < b.length; i++) { + final int idx = i * 2; + final int h = Character.digit( hexstr.charAt( offset + idx ), 16 ); + final int l = Character.digit( hexstr.charAt( offset + idx + 1 ), 16 ); + if( 0 <= h && 0 <= l ) { + b[i] = (byte) ( (h << 4) + l ); + } else { + // invalid char + final byte[] b2 = new byte[i]; + System.arraycopy(b, 0, b2, 0, i); + return b2; + } + } + } else { + int i=0; + for (int idx = (b.length-1)*2; idx >= 0; idx-=2, i++) { + final int h = Character.digit( hexstr.charAt( offset + idx ), 16); + final int l = Character.digit( hexstr.charAt( offset + idx + 1 ), 16); + if( 0 <= h && 0 <= l ) { + b[i] = (byte) ( (h << 4) + l ); + } else { + // invalid char + final byte[] b2 = new byte[i]; + System.arraycopy(b, 0, b2, 0, i); + return b2; + } + } + } + return b; + } + + /** * Produce a lower-case hexadecimal string representation of the given byte values. * <p> * If lsbFirst is true, orders LSB left -> MSB right, usual for byte streams. Result will not have a leading `0x`.<br> @@ -80,10 +132,11 @@ public class BasicTypes { hexChars = new char[2 + byte_len * 2]; hexChars[0] = '0'; hexChars[1] = 'x'; - for (int j = byte_len-1; j >= 0; j--) { + int k=0; + for (int j = byte_len-1; j >= 0; j--, k++) { final int v = bytes[offset + j] & 0xFF; - hexChars[char_offset + j * 2] = hex_array[v >>> 4]; - hexChars[char_offset + j * 2 + 1] = hex_array[v & 0x0F]; + hexChars[char_offset + k * 2] = hex_array[v >>> 4]; + hexChars[char_offset + k * 2 + 1] = hex_array[v & 0x0F]; } } return new String(hexChars); |