Convert byte array To Hex String via Character.forDigit - Android java.lang

Android examples for java.lang:Byte Array

Description

Convert byte array To Hex String via Character.forDigit

Demo Code

public class Main{

    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("0x");
        if (src == null || src.length <= 0) {
            return null;
        }//  ww w .  ja v  a 2 s.c o  m
        char[] buffer = new char[2];
        for (int i = 0; i < src.length; i++) {
            buffer[0] = Character.forDigit((src[i] >>> 4) & 0x0F, 16);
            buffer[1] = Character.forDigit(src[i] & 0x0F, 16);
            stringBuilder.append(buffer);
        }
        return stringBuilder.toString();
    }

}

Related Tutorials