• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.carrierapi.cts;
18 
19 import javax.annotation.Nonnull;
20 
21 /**
22  * Utility class for converting between hex Strings and bitwise representations.
23  */
24 public class IccUtils {
25 
26     // A table mapping from a number to a hex character for fast encoding hex strings.
27     private static final char[] HEX_CHARS = {
28             '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
29     };
30 
31     @Nonnull
bytesToHexString(byte[] bytes)32     public static String bytesToHexString(byte[] bytes) {
33         StringBuilder ret = new StringBuilder(2 * bytes.length);
34         for (int i = 0 ; i < bytes.length ; i++) {
35             int b;
36             b = 0x0f & (bytes[i] >> 4);
37             ret.append(HEX_CHARS[b]);
38             b = 0x0f & bytes[i];
39             ret.append(HEX_CHARS[b]);
40         }
41         return ret.toString();
42     }
43 
44     /**
45      * Converts a hex String to a byte array.
46      *
47      * @param s A string of hexadecimal characters, must be an even number of
48      *          chars long
49      *
50      * @return byte array representation
51      *
52      * @throws RuntimeException on invalid format
53      */
hexStringToBytes(String s)54     public static byte[] hexStringToBytes(String s) {
55         byte[] ret;
56 
57         if (s == null) return null;
58 
59         int sz = s.length();
60 
61         ret = new byte[sz/2];
62 
63         for (int i=0 ; i <sz ; i+=2) {
64             ret[i/2] = (byte) ((hexCharToInt(s.charAt(i)) << 4) | hexCharToInt(s.charAt(i+1)));
65         }
66 
67         return ret;
68     }
69 
70     /**
71      * Converts a hex char to its integer value
72      *
73      * @param c A single hexadecimal character. Must be in one of these ranges:
74      *          - '0' to '9', or
75      *          - 'a' to 'f', or
76      *          - 'A' to 'F'
77      *
78      * @return the integer representation of {@code c}
79      *
80      * @throws RuntimeException on invalid character
81      */
hexCharToInt(char c)82     public static int hexCharToInt(char c) {
83         if (c >= '0' && c <= '9') return (c - '0');
84         if (c >= 'A' && c <= 'F') return (c - 'A' + 10);
85         if (c >= 'a' && c <= 'f') return (c - 'a' + 10);
86 
87         throw new RuntimeException ("invalid hex char '" + c + "'");
88     }
89 }
90