Android String to Hex String Convert toHex(String txt)

Here you can find the source of toHex(String txt)


to Hex


public static String toHex(String txt) 

Method Source Code

//package com.java2s;

public class Main {
    private final static String HEX = "0123456789ABCDEF";

    public static String toHex(String txt) {
        return toHex(txt.getBytes());
    }/*from ww w. j  a v  a2  s .co m*/

    public static String toHex(byte[] buf) {
        if (buf == null)
            return "";
        StringBuffer result = new StringBuffer(2 * buf.length);
        for (int i = 0; i < buf.length; i++) {
            appendHex(result, buf[i]);
        return result.toString();

    private static void appendHex(StringBuffer sb, byte b) {
        sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));


  1. toHex(byte[] buf)
  2. toHexString(String str)
  3. hexStr2Str(String hexStr)
  4. hexStringToCommonString(String hexString)