am 979f9f99: Merge "Consolidate hex string utils"

* commit '979f9f99394535c81a3b7eaa6f947a04d9ad7b3c':
  Consolidate hex string utils
main
Satoshi Kataoka 2013-08-18 22:34:32 -07:00 committed by Android Git Automerger
commit 551f24eeed
1 changed files with 7 additions and 0 deletions

View File

@ -370,12 +370,19 @@ public final class StringUtils {
return sb.toString(); return sb.toString();
} }
/**
* Convert hex string to byte array. The string length must be an even number.
*/
@UsedForTesting @UsedForTesting
public static byte[] hexStringToByteArray(String hexString) { public static byte[] hexStringToByteArray(String hexString) {
if (TextUtils.isEmpty(hexString)) { if (TextUtils.isEmpty(hexString)) {
return null; return null;
} }
final int N = hexString.length(); final int N = hexString.length();
if (N % 2 != 0) {
throw new NumberFormatException("Input hex string length must be an even number."
+ " Length = " + N);
}
final byte[] bytes = new byte[N / 2]; final byte[] bytes = new byte[N / 2];
for (int i = 0; i < N; i += 2) { for (int i = 0; i < N; i += 2) {
bytes[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) bytes[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4)