Convert byte to hex string and Integer.toHexString - Android java.lang

Android examples for java.lang:Hex

Description

Convert byte to hex string and Integer.toHexString

Demo Code

import android.annotation.SuppressLint;
import android.text.TextUtils;
import java.util.Collection;
import java.util.List;

public class Main{


    @SuppressLint("DefaultLocale")
    public static String byte2hex(byte[] b) {
        StringBuffer hs = new StringBuffer();
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1)
                hs.append("0").append(stmp);
            else/*w  w w .j a  v  a2s.com*/
                hs.append(stmp);
        }
        return hs.toString();
    }

}

Related Tutorials