Convenience method to convert a byte to a hex string. : Hex « Date Type « Android






Convenience method to convert a byte to a hex string.

 
class Main{
  /**
   * Convenience method to convert a byte to a hex string.
   * 
   * @param data
   *            the byte to convert
   * @return String the converted byte
   */
  public static String byteToHex(byte data) {
    StringBuffer buf = new StringBuffer();
    buf.append(toHexChar((data >>> 4) & 0x0F));
    buf.append(toHexChar(data & 0x0F));
    return buf.toString();
  }
  /**
   * Convenience method to convert an int to a hex char.
   * 
   * @param i
   *            the int to convert
   * @return char the converted char
   */
  public static char toHexChar(int i) {
    if ((0 <= i) && (i <= 9))
      return (char) ('0' + i);
    else
      return (char) ('a' + (i - 10));
  }
}

   
  








Related examples in the same category

1.Hex Dump
2.converts given byte array to a hex string
3.converts given hex string to a byte array (ex: "0D0A" => {0x0D, 0x0A,})
4.Color name and its hex value
5.hex To Decimal
6.Get Hex string out of byte array
7.get Brightness, get Hex Name
8.String to Hex
9.Convenience method to convert a byte array to a hex string.
10.Convert a byte[] array to readable string format. This makes the "hex" readable!
11.Decode Unicode Hex
12.byte Array To Hex String