Here you can find the source of toHexString(byte[] bytes)
Given an array of bytes, it will return the representation as a string of hex data.
Parameter | Description |
---|---|
bytes | - the bytes to convert to hex |
bytes
public static String toHexString(byte[] bytes)
//package com.java2s; public class Main { private static final char[] HEX_CHARS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; /**/*from www. j av a 2 s .c om*/ * <p>Given an array of bytes, it will return the representation as a string of hex data. * This is primarily useful when needing to display encrypted data as a readable string.</p> * * @param bytes - the bytes to convert to hex * @return a hex representation of the given byte array <code>bytes</code> */ public static String toHexString(byte[] bytes) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < bytes.length; i++) { byte b = bytes[i]; sb.append(new char[] { HEX_CHARS[(b >> 4) & 0x0f], HEX_CHARS[b & 0x0f] }); } return sb.toString(); } }