Java Hex Calculate toHexFromByte(final byte b)

Here you can find the source of toHexFromByte(final byte b)

Description

Takes the given input and returns a String containing the hex representation of the byte.

License

Open Source License

Parameter

Parameter Description
b byte the byte to be converted

Return

String the hex conversion of the byte (2 characters)

Declaration

public static String toHexFromByte(final byte b) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /** Allowable hex values */
    private final static String[] hexSymbols = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c",
            "d", "e", "f" };
    public final static int BITS_PER_HEX_DIGIT = 4;

    /**/*  ww w.j  a  v  a2 s .c  o m*/
     * Takes the given input and returns a String containing the hex representation of the byte.
     *
     * @param b
     *            byte the byte to be converted
     *
     * @return String the hex conversion of the byte (2 characters)
     */
    public static String toHexFromByte(final byte b) {
        // need to and the shift result as java maintains the
        // sign bit and puts it back after the shift
        byte leftSymbol = (byte) ((b >>> BITS_PER_HEX_DIGIT) & 0x0f);
        byte rightSymbol = (byte) (b & 0x0f);

        return (hexSymbols[leftSymbol] + hexSymbols[rightSymbol]);
    }
}

Related

  1. toHexDump(byte[] bytes)
  2. toHexEscape(final int u0)
  3. toHexFilter(String inAscii)
  4. toHexFromBin(final String binSymbols)
  5. toHexFromByte(byte b)
  6. toHexFromBytes(byte[] bytes)
  7. toHexFromBytes(final byte[] bytes)
  8. toHexFromOct(final String octSymbols)
  9. toHexHashString(byte[] id)