Java Byte to Hex String byteToHexString(byte data)

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

Description

convert a byte to a hex string

License

Open Source License

Parameter

Parameter Description
data array

Return

String representation in hex

Declaration

public static String byteToHexString(byte data) 

Method Source Code

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

public class Main {
    /**//from   w w  w . j a va 2s  .c  o m
     * convert a byte to a hex string
     *
     * @param data array
     * @return String representation in hex
     */
    public static String byteToHexString(byte data) {

        StringBuffer buf = new StringBuffer();
        buf.append(toHexChar((data >>> 4) & 0x0F));
        buf.append(toHexChar(data & 0x0F));

        return buf.toString();
    }

    /**
     * convert a integer into a hexadecimal character
     *
     * @param i integer
     * @return hex char
     */
    public static char toHexChar(int i) {
        if ((0 <= i) && (i <= 9)) {
            return (char) ('0' + i);
        } else {
            return (char) ('a' + (i - 10));
        }
    }
}

Related

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