Convert byte array to Hex String with hardcoded hex digit array - Android java.lang

Android examples for java.lang:Byte Array

Description

Convert byte array to Hex String with hardcoded hex digit array

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{

    private static String toHexString(byte[] b) { // String to byte
        char HEX_DIGITS[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
                '9', 'A', 'B', 'C', 'D', 'E', 'F' };

        StringBuilder sb = new StringBuilder(b.length * 2);
        for (int i = 0; i < b.length; i++) {
            sb.append(HEX_DIGITS[(b[i] & 0xf0) >>> 4]);
            sb.append(HEX_DIGITS[b[i] & 0x0f]);
        }/*  w  w  w .  ja v  a2  s .  c  o  m*/
        return sb.toString();
    }

}

Related Tutorials