Android Utililty Methods String to Hex String Convert

List of utility methods to do String to Hex String Convert

Description

The list of methods to do String to Hex String Convert are organized into topic(s).

Method

StringtoHex(String txt)
to Hex
return toHex(txt.getBytes());
StringtoHex(byte[] buf)
to Hex
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();
StringtoHexString(String str)
to Hex String
byte[] byteArray;
try {
    byteArray = str.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    byteArray = str.getBytes();
return bytesToHexString(byteArray);
...
StringhexStr2Str(String hexStr)
hex Str Str
String str = "0123456789ABCDEF";
char[] hexs = hexStr.toCharArray();
byte[] bytes = new byte[hexStr.length() / 2];
int n;
for (int i = 0; i < bytes.length; i++) {
    n = str.indexOf(hexs[2 * i]) * 16;
    n += str.indexOf(hexs[2 * i + 1]);
    bytes[i] = (byte) (n & 0xff);
...
StringhexStringToCommonString(String hexString)
hex String To Common String
byte[] bytes = hexStringToBytes(hexString);
try {
    return new String(bytes, "gbk");
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    return new String(bytes);
StringtoHex(String txt)
to Hex
return toHex(txt.getBytes());
StringtoHex(String txt)
Retrieve a string's hex format
return toHex(txt.getBytes());
byte[]parseHexStr2Byte(String hexStr)
parse Hex Str Byte
if (hexStr.length() < 1)
    return null;
byte[] result = new byte[hexStr.length() / 2];
for (int i = 0; i < hexStr.length() / 2; i++) {
    int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1),
            16);
    int low = Integer.parseInt(
            hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
...
byte[]stringToHex(String string)
Converts the string to a hex representation with leading 0 byte and 2 null terminating byte
ByteArrayOutputStream stream = new ByteArrayOutputStream();
byte name_bytes_temp[] = string.getBytes();
int length = name_bytes_temp.length;
for (int i = 0; i < (length + 1) * 2; ++i) {
    if (i % 2 == 1 && (i - 1) / 2 < length) {
        stream.write(name_bytes_temp[(i - 1) / 2]);
    } else
        stream.write((byte) 0);
...
Stringstr2HexStr(String str)
str Hex Str
char[] chars = "0123456789ABCDEF".toCharArray();
StringBuilder sb = new StringBuilder("");
byte[] bs = str.getBytes();
int bit;
for (int i = 0; i < bs.length; i++) {
    bit = (bs[i] & 0x0f0) >> 4;
    sb.append("0x");
    sb.append(chars[bit]);
...