Android String to Hex String Convert toHex(String txt)

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

Description

to Hex

Declaration

public static String toHex(String txt) 

Method Source Code

//package com.java2s;

public class Main {
    public static String toHex(String txt) {
        return toHex(txt.getBytes());
    }//from   w ww  .j av  a2  s  .c  om

    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) {
        final String HEX = "0123456789ABCDEF";
        sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));
    }
}

Related

  1. toHex(String txt)
  2. toHex(byte[] buf)
  3. toHexString(String str)
  4. hexStr2Str(String hexStr)
  5. hexStringToCommonString(String hexString)
  6. toHex(String txt)
  7. parseHexStr2Byte(String hexStr)
  8. stringToHex(String string)
  9. str2HexStr(String str)