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

Android examples for java.lang:Byte Array

Description

Convert byte array to hex string using Integer.toHexString

Demo Code

import android.util.Base64;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Vector;
import java.util.regex.Pattern;

public class Main{

    /**/* w  ww . ja v  a 2s .  c  om*/
     * Convert byte[] to hex string.
     */
    public static String bytes2HexString(byte[] data) {
        String ret = "";
        for (int i = 0; i < data.length; i++) {
            String hex = Integer.toHexString(data[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            ret += hex.toUpperCase();
        }
        return ret;
    }

}

Related Tutorials