Android Utililty Methods Byte Array to Hex Convert

List of utility methods to do Byte Array to Hex Convert

Description

The list of methods to do Byte Array to Hex Convert are organized into topic(s).

Method

Stringbyte2hex(byte[] b)
bytehex
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
    stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
    if (stmp.length() == 1)
        hs = hs + "0" + stmp;
    else
        hs = hs + stmp;
...
StringhexEncode(byte[] data)
hex Encode
StringBuffer r = new StringBuffer();
for (int i = 0; i < data.length; i++) {
    byte v = data[i];
    r.append(HEXCHARS[(v >> 4) & 0xf]).append(HEXCHARS[v & 0xf]);
return r.toString();
StringencodeHex(byte[] data)
byte[] to hex
int l = data.length;
char[] out = new char[l << 1];
for (int i = 0, j = 0; i < l; i++) {
    out[j++] = DIGITS_LOWER[(0xF0 & data[i]) >>> 4];
    out[j++] = DIGITS_LOWER[0x0F & data[i]];
return new String(out);
byte[]decodeHex(char[] data)
hex to byte[]
int len = data.length;
if ((len & 0x01) != 0) {
    throw new RuntimeException("Odd number of characters.");
byte[] out = new byte[len >> 1];
for (int i = 0, j = 0; j < len; i++) {
    int f = toDigit(data[j], j) << 4;
    j++;
...
StringbytesToHex(byte[] data)
bytes To Hex
if (data == null) {
    return null;
int len = data.length;
String str = "";
for (int i = 0; i < len; i++) {
    if ((data[i] & 0xFF) < 16)
        str = str + "0"
...
StringBuilderbytesToHexString(byte[] bytesArray)
bytes To Hex String
if (bytesArray == null) {
    return null;
StringBuilder sBuilder = new StringBuilder();
for (byte b : bytesArray) {
    String hv = String.format("%02x", b);
    sBuilder.append(hv);
return sBuilder;
StringparseByte2HexStr(byte buf[])
parse Byte Hex Str
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
    String hex = Integer.toHexString(buf[i] & 0xFF);
    if (hex.length() == 1) {
        hex = '0' + hex;
    sb.append(hex.toUpperCase());
return sb.toString();
StringbytesToHex(byte[] bytes)
Takes the provided byte array and converts it into a hexidecimal string with two characters per byte.
return bytesToHex(bytes, false);
StringbytesToHex(byte[] bytes, boolean withSpaces)
Takes the provided byte array and converts it into a hexidecimal string with two characters per byte.
StringBuilder sb = new StringBuilder();
for (byte hashByte : bytes) {
    int intVal = 0xff & hashByte;
    if (intVal < 0x10) {
        sb.append('0');
    sb.append(Integer.toHexString(intVal));
    if (withSpaces) {
...
StringbyteArrayToHexString(byte[] bytes)
byte Array To Hex String
char[] hexChars = new char[bytes.length * 2];
int v;
for (int j = 0; j < bytes.length; j++) {
    v = bytes[j] & 0xFF;
    hexChars[j * 2] = hexArray[v >>> 4];
    hexChars[j * 2 + 1] = hexArray[v & 0x0F];
return new String(hexChars);
...