Here you can find the source of bytesToHexString(final byte[] bytes)
Parameter | Description |
---|---|
bytes | a parameter |
public static String bytesToHexString(final byte[] bytes)
//package com.java2s; //License from project: Apache License public class Main { /**/*from ww w.j a v a2s . c o m*/ * Converts the specified bytes array into its hex string representation. * * @param bytes * @return null */ public static String bytesToHexString(final byte[] bytes) { if (bytes == null) return null; char[] chars = new char[bytes.length * 2]; for (int byteIndex = 0, charIndex = 0; byteIndex < bytes.length; ++byteIndex, charIndex += 2) { String str = Integer.toString(bytes[byteIndex] + 128, 16); str = ensureLength(str, 2, false, '0'); chars[charIndex] = str.charAt(0); chars[charIndex + 1] = str.charAt(1); } return new String(chars); } /** * Add chars if str's length not enougth * @param str The string * @param minLength the min length * @param appendToRight append to right * @param appendChar the appended char * @return the string with length >= minLength */ public static String ensureLength(String str, int minLength, boolean appendToRight, char appendChar) { if (str == null) str = "null"; else if (appendToRight) { str = str + createString(Math.max(minLength, str.length()) - str.length(), appendChar); } else { str = createString(Math.max(minLength, str.length()) - str.length(), appendChar) + str; } return str; } /** * Generate the String that has chr character duplicated num number of times * @param num The number of character * @param chr The character to be duplicated * @return the String that has chr character duplicated num number of times */ public static String createString(int num, char chr) { char[] chs = new char[num]; for (int i = 0; i < chs.length; i++) { chs[i] = chr; } return new String(chs); } }