From f27d080d1aa101fa4407d2b3ec6b4d1a4a0e5787 Mon Sep 17 00:00:00 2001 From: Dan Cashman Date: Thu, 8 Feb 2018 16:53:58 -0800 Subject: ByteStringUtils: fix hex parsing to include lowercase input. fromHexToByteArray() should be able to handle any hex input, not just input specified as upper-case. Bug: 73001469 Test: Builds, boots. Change-Id: I89dcbaeb95d6fc540b532d897ff27fb5f40381c7 --- core/java/android/util/ByteStringUtils.java | 92 +++++++++++++++-------------- 1 file changed, 47 insertions(+), 45 deletions(-) (limited to 'core/java/android/util/ByteStringUtils.java') diff --git a/core/java/android/util/ByteStringUtils.java b/core/java/android/util/ByteStringUtils.java index 333208db5f79..f6460ad90f6b 100644 --- a/core/java/android/util/ByteStringUtils.java +++ b/core/java/android/util/ByteStringUtils.java @@ -22,61 +22,63 @@ package android.util; * @hide */ public final class ByteStringUtils { - private final static char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray(); + private static final char[] HEX_LOWERCASE_ARRAY = "0123456789abcdef".toCharArray(); + private static final char[] HEX_UPPERCASE_ARRAY = "0123456789ABCDEF".toCharArray(); - private ByteStringUtils() { + private ByteStringUtils() { /* hide constructor */ - } - - /** - * Returns the hex encoded string representation of bytes. - * @param bytes Byte array to encode. - * @return Hex encoded string representation of bytes. - */ - public static String toHexString(byte[] bytes) { - if (bytes == null || bytes.length == 0 || bytes.length % 2 != 0) { - return null; } - final int byteLength = bytes.length; - final int charCount = 2 * byteLength; - final char[] chars = new char[charCount]; + /** + * Returns the hex encoded string representation of bytes. + * @param bytes Byte array to encode. + * @return Hex encoded string representation of bytes. + */ + public static String toHexString(byte[] bytes) { + if (bytes == null || bytes.length == 0 || bytes.length % 2 != 0) { + return null; + } - for (int i = 0; i < byteLength; i++) { - final int byteHex = bytes[i] & 0xFF; - chars[i * 2] = HEX_ARRAY[byteHex >>> 4]; - chars[i * 2 + 1] = HEX_ARRAY[byteHex & 0x0F]; - } - return new String(chars); - } + final int byteLength = bytes.length; + final int charCount = 2 * byteLength; + final char[] chars = new char[charCount]; - /** - * Returns the decoded byte array representation of str. - * @param str Hex encoded string to decode. - * @return Decoded byte array representation of str. - */ - public static byte[] fromHexToByteArray(String str) { - if (str == null || str.length() == 0 || str.length() % 2 != 0) { - return null; + for (int i = 0; i < byteLength; i++) { + final int byteHex = bytes[i] & 0xFF; + chars[i * 2] = HEX_UPPERCASE_ARRAY[byteHex >>> 4]; + chars[i * 2 + 1] = HEX_UPPERCASE_ARRAY[byteHex & 0x0F]; + } + return new String(chars); } - final char[] chars = str.toCharArray(); - final int charLength = chars.length; - final byte[] bytes = new byte[charLength / 2]; + /** + * Returns the decoded byte array representation of str. + * @param str Hex encoded string to decode. + * @return Decoded byte array representation of str. + */ + public static byte[] fromHexToByteArray(String str) { + if (str == null || str.length() == 0 || str.length() % 2 != 0) { + return null; + } + + final char[] chars = str.toCharArray(); + final int charLength = chars.length; + final byte[] bytes = new byte[charLength / 2]; - for (int i = 0; i < bytes.length; i++) { - bytes[i] = - (byte)(((getIndex(chars[i * 2]) << 4) & 0xF0) | (getIndex(chars[i * 2 + 1]) & 0x0F)); + for (int i = 0; i < bytes.length; i++) { + bytes[i] = + (byte) (((getIndex(chars[i * 2]) << 4) & 0xF0) + | (getIndex(chars[i * 2 + 1]) & 0x0F)); + } + return bytes; } - return bytes; - } - private static int getIndex(char c) { - for (int i = 0; i < HEX_ARRAY.length; i++) { - if (HEX_ARRAY[i] == c) { - return i; - } + private static int getIndex(char c) { + for (int i = 0; i < HEX_UPPERCASE_ARRAY.length; i++) { + if (HEX_UPPERCASE_ARRAY[i] == c || HEX_LOWERCASE_ARRAY[i] == c) { + return i; + } + } + return -1; } - return -1; - } } -- cgit v1.2.3