summaryrefslogtreecommitdiff
path: root/core/java/android/util/ByteStringUtils.java
diff options
context:
space:
mode:
authorDan Cashman <dcashman@google.com>2018-02-08 16:53:58 -0800
committerDan Cashman <dcashman@google.com>2018-02-08 17:06:05 -0800
commitf27d080d1aa101fa4407d2b3ec6b4d1a4a0e5787 (patch)
tree7c8e576777cdb4793996a7f3f17ad1a0187ea09b /core/java/android/util/ByteStringUtils.java
parent1c12690fc69c7d95e1c8a003e075a8074f8b58ba (diff)
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
Diffstat (limited to 'core/java/android/util/ByteStringUtils.java')
-rw-r--r--core/java/android/util/ByteStringUtils.java92
1 files changed, 47 insertions, 45 deletions
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;
- }
}