Here you can find the source of toHexString(int[] x, int bitsPerDigit)
static String toHexString(int[] x, int bitsPerDigit)
//package com.java2s; /**//from w ww .j a v a2 s . c o m * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ public class Main { static String toHexString(int[] x, int bitsPerDigit) { final int digitsPerLong = (59 / bitsPerDigit) + 1; final StringBuilder b = new StringBuilder(); for (int i = 0; i < x.length;) { long a = 0; int j = 0; for (; i < x.length && j < digitsPerLong; j++) { a |= ((long) x[i] << (bitsPerDigit * j)); i++; } b.insert(0, String.format((i == x.length ? "%" : "%0" + ((bitsPerDigit * j + 3) >> 2)) + "X ", a)); } return b.toString(); } static String toHexString(byte[] bytes, final int digitsPerGroup, final String groupSeparator) { if (digitsPerGroup <= 0 || (digitsPerGroup & 1) != 0) { throw new IllegalArgumentException( "digitsPerGroup <= 0 || (digitsPerGroup & 1) != 0, digitsPerGroup=" + digitsPerGroup); } final StringBuilder b = new StringBuilder(); int r = 0; for (int i = bytes.length - 1; i >= 0; i--) { b.append(String.format("%2X", bytes[i])); if ((r += 2) == digitsPerGroup && i >= 0) { r = 0; b.append(groupSeparator); } } return b.toString(); } static String toString(int[] x) { if (x == null) { return "null"; } else if (x.length == 0) { return "<empty>"; } else { final StringBuilder b = new StringBuilder("["); b.append(x[0]); for (int i = 1; i < x.length; i++) { b.append(", ").append(x[i]); } return b.append("]").toString(); } } static String toString(int[] x, int length, int M) { final int hexPerDigit = (M + 3) >> 2; final StringBuilder b = new StringBuilder(); for (int i = length - 1; i >= 0; i--) { b.append(String.format(" %0" + hexPerDigit + "X", x[i])); } return b.toString(); } }