Here you can find the source of byteToHexString(byte in)
Parameter | Description |
---|---|
in | byte[] buffer to convert to string format |
public static String byteToHexString(byte in)
//package com.java2s; /**/*w w w .j a v a 2s . c o m*/ * (C) 2011-2012 Alibaba Group Holding Limited. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * version 2 as published by the Free Software Foundation. * */ public class Main { /** All hexadecimal characters */ final static String hexChars[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" }; /** * Converts a byte to readable hexadecimal format in a string<br> * This code was originally taken from Jeff Boyle's article on devX.com * * @param in * byte[] buffer to convert to string format * @return a String containing the hex values corresponding to the input * byte, all attached */ public static String byteToHexString(byte in) { StringBuffer out = new StringBuffer(2); // Strip off high nibble byte ch = (byte) (in & 0xF0); // shift the bits down ch = (byte) (ch >>> 4); ch = (byte) (ch & 0x0F); // must do this is high order bit is on! out.append(hexChars[(int) ch]); // convert the nibble to a String // Character ch = (byte) (in & 0x0F); // Strip off low nibble out.append(hexChars[(int) ch]); // convert the nibble to a String // Character return out.toString(); } }