convert byte array To Hex - Android java.lang

Android examples for java.lang:Hex

Description

convert byte array To Hex

Demo Code

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main{

    private static String convertToHex(byte[] data) {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < data.length; i++) {
            int halfbyte = (data[i] >>> 4) & 0x0F;
            int two_halfs = 0;
            do {/* w ww .j a va  2 s  . co  m*/
                if ((0 <= halfbyte) && (halfbyte <= 9)) {
                    buf.append((char) ('0' + halfbyte));
                } else {
                    buf.append((char) (('a') + (halfbyte - 10)));
                }
            } while (two_halfs++ < 1);
        }
        return buf.toString();
    }

}

Related Tutorials