Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2010 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 libcore.util;
     18 
     19 /**
     20  * Converts integral types to strings. This class is public but hidden so that it can also be
     21  * used by java.util.Formatter to speed up %d. This class is in java.lang so that it can take
     22  * advantage of the package-private String constructor.
     23  *
     24  * The most important methods are appendInt/appendLong and intToString(int)/longToString(int).
     25  * The former are used in the implementation of StringBuilder, StringBuffer, and Formatter, while
     26  * the latter are used by Integer.toString and Long.toString.
     27  *
     28  * The append methods take AbstractStringBuilder rather than Appendable because the latter requires
     29  * CharSequences, while we only have raw char[]s. Since much of the savings come from not creating
     30  * any garbage, we can't afford temporary CharSequence instances.
     31  *
     32  * One day the performance advantage of the binary/hex/octal specializations will be small enough
     33  * that we can lose the duplication, but until then this class offers the full set.
     34  *
     35  * @hide
     36  */
     37 public final class IntegralToString {
     38     /**
     39      * The digits for every supported radix.
     40      */
     41     private static final char[] DIGITS = {
     42         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
     43         'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
     44         'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
     45         'u', 'v', 'w', 'x', 'y', 'z'
     46     };
     47 
     48     private static final char[] UPPER_CASE_DIGITS = {
     49         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
     50         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
     51         'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
     52         'U', 'V', 'W', 'X', 'Y', 'Z'
     53     };
     54 
     55     private IntegralToString() {
     56     }
     57 
     58     public static String bytesToHexString(byte[] bytes, boolean upperCase) {
     59         char[] digits = upperCase ? UPPER_CASE_DIGITS : DIGITS;
     60         char[] buf = new char[bytes.length * 2];
     61         int c = 0;
     62         for (byte b : bytes) {
     63             buf[c++] = digits[(b >> 4) & 0xf];
     64             buf[c++] = digits[b & 0xf];
     65         }
     66         return new String(buf);
     67     }
     68 }
     69