Java Byte to Hex String byteToHexString(byte in)

Here you can find the source of byteToHexString(byte in)

Description

Converts a byte to readable hexadecimal format in a string
This code was originally taken from Jeff Boyle's article on devX.com

License

Open Source License

Parameter

Parameter Description
in byte[] buffer to convert to string format

Return

a String containing the hex values corresponding to the input byte, all attached

Declaration

public static String byteToHexString(byte in) 

Method Source Code

//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();
    }
}

Related

  1. byteToHexString(byte b)
  2. byteToHexString(byte bytes[])
  3. byteToHexString(byte data)
  4. byteToHexString(byte data)
  5. byteToHexString(byte ib)
  6. byteToHexString(byte num)
  7. byteToHexString(byte src)
  8. byteToHexString(byte value)
  9. byteToHexString(byte value)