Home | History | Annotate | Download | only in conscrypt
      1 /*
      2  * Copyright 2013 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 org.conscrypt;
     18 
     19 /**
     20  *
     21  * Helper class for dealing with hexadecimal strings.
     22  *
     23  */
     24 // public for testing by TrustedCertificateStoreTest
     25 public class Hex {
     26     private Hex() {}
     27 
     28     private final static char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
     29 
     30     public static String bytesToHexString(byte[] bytes) {
     31         char[] buf = new char[bytes.length * 2];
     32         int c = 0;
     33         for (byte b : bytes) {
     34             buf[c++] = DIGITS[(b >> 4) & 0xf];
     35             buf[c++] = DIGITS[b & 0xf];
     36         }
     37         return new String(buf);
     38     }
     39 
     40     public static String intToHexString(int i, int minWidth) {
     41         int bufLen = 8;  // Max number of hex digits in an int
     42         char[] buf = new char[bufLen];
     43         int cursor = bufLen;
     44 
     45         do {
     46             buf[--cursor] = DIGITS[i & 0xf];
     47         } while ((i >>>= 4) != 0 || (bufLen - cursor < minWidth));
     48 
     49         return new String(buf, cursor, bufLen - cursor);
     50     }
     51 
     52 }
     53