Convert byte or byte array to hex String - Android java.lang

Android examples for java.lang:Hex

Description

Convert byte or byte array to hex String

Demo Code

import android.content.Context;

public class Main{

    public static final String hexString(byte byte0) {
        char ac[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'A', 'B', 'C', 'D', 'E', 'F' };
        char ac1[] = new char[2];
        ac1[0] = ac[byte0 >>> 4 & 0xf];
        ac1[1] = ac[byte0 & 0xf];
        String s = new String(ac1);
        return s;
    }/*from   w  ww.java2  s  . c o m*/
    public static final String hexString(byte[] bytes) {
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            buffer.append(hexString(bytes[i]));
        }
        return buffer.toString();
    }

}

Related Tutorials