Here you can find the source of bytesToHex(byte[] bytes)
Parameter | Description |
---|---|
bytes | raw binary data |
public static String bytesToHex(byte[] bytes)
//package com.java2s; /*// w w w. ja va2 s . c om * Copyright (c) 2011-2013 Nexmo Inc * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ 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', }; /** * translate a byte array of raw data into a String with a hex representation of that data * * @param bytes raw binary data * * @return String Hex representation of the raw data */ public static String bytesToHex(byte[] bytes) { return bytesToHex(bytes, null); } /** * translate a byte array of raw data into a String with a hex representation of that data. * Each octet will be separated with a specific separator. * * @param bytes raw binary data * @param separator This string will be injected into the output inbetween each octet in the stream * * @return String Hex representation of the raw data with each octet separated by 'separator' */ public static String bytesToHex(byte[] bytes, String separator) { StringBuilder tmpBuffer = new StringBuilder(); if (bytes != null) { for (byte c : bytes) { int b = c; if (b < 0) b += 256; if (separator != null) tmpBuffer.append(separator); tmpBuffer.append(HEX_CHARS[(b & 0xf0) / 0x10]); // note, this benchmarks faster than using >> 4 tmpBuffer.append(HEX_CHARS[b & 0x0f]); } } return tmpBuffer.toString(); } }