Java examples for java.lang:byte Array to hex
Returns a hexadecimal representation of the given byte array.
//package com.java2s; public class Main { public static void main(String[] argv) throws Exception { byte[] bytes = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 }; System.out.println(asHex(bytes)); }//from ww w .j a va2 s .c om /** * Returns a hexadecimal representation of the given byte array. * * @param bytes * the array to output to an hex string * @return the hex representation as a string */ public static String asHex(byte[] bytes) { return asHex(bytes, null); } /** * Returns a hexadecimal representation of the given byte array. * * @param bytes * the array to output to an hex string * @param separator * the separator to use between each byte in the output string. If null no char is inserted between each byte value. * @return the hex representation as a string */ public static String asHex(byte[] bytes, String separator) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < bytes.length; i++) { String code = Integer.toHexString(bytes[i] & 0xFF); if ((bytes[i] & 0xFF) < 16) { sb.append('0'); } sb.append(code); if (separator != null && i < bytes.length - 1) { sb.append(separator); } } return sb.toString(); } }