1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package jdk.test.lib;
  25 
  26 import java.math.BigInteger;
  27 
  28 /**
  29  * Utility class containing conversions between strings, arrays, and numeric
  30  * values.
  31  */
  32 
  33 public class Convert {
  34 
  35     // Convert from a byte array to a hexadecimal representation as a string.
  36     public static String byteArrayToHexString(byte[] arr) {
  37         StringBuilder result = new StringBuilder();
  38         for (int i = 0; i < arr.length; ++i) {
  39             byte curVal = arr[i];
  40             result.append(Character.forDigit(curVal >> 4 & 0xF, 16));
  41             result.append(Character.forDigit(curVal & 0xF, 16));
  42         }
  43         return result.toString();
  44     }
  45 
  46     // Expand a single byte to a byte array
  47     public static byte[] byteToByteArray(byte v, int length) {
  48         byte[] result = new byte[length];
  49         result[0] = v;
  50         return result;
  51     }
  52 
  53     // Convert a hexadecimal string to a byte array
  54     public static byte[] hexStringToByteArray(String str) {
  55         byte[] result = new byte[str.length() / 2];
  56         for (int i = 0; i < result.length; i++) {
  57             result[i] = (byte) Character.digit(str.charAt(2 * i), 16);
  58             result[i] <<= 4;
  59             result[i] += Character.digit(str.charAt(2 * i + 1), 16);
  60         }
  61         return result;
  62     }
  63 
  64     /*
  65      * Convert a hexadecimal string to the corresponding little-ending number
  66      * as a BigInteger. The clearHighBit argument determines whether the most
  67      * significant bit of the highest byte should be set to 0 in the result.
  68      */
  69     public static
  70     BigInteger hexStringToBigInteger(boolean clearHighBit, String str) {
  71         BigInteger result = BigInteger.ZERO;
  72         for (int i = 0; i < str.length() / 2; i++) {
  73             int curVal = Character.digit(str.charAt(2 * i), 16);
  74             curVal <<= 4;
  75             curVal += Character.digit(str.charAt(2 * i + 1), 16);
  76             if (clearHighBit && i == str.length() / 2 - 1) {
  77                 curVal &= 0x7F;
  78             }
  79             result = result.add(BigInteger.valueOf(curVal).shiftLeft(8 * i));
  80         }
  81         return result;
  82     }
  83 }
  84 
  85