Android String to Hex String Convert toHexString(String str)

Here you can find the source of toHexString(String str)

Description

to Hex String

Declaration

public static String toHexString(String str) 

Method Source Code

//package com.java2s;
import java.io.UnsupportedEncodingException;

public class Main {

    public static String toHexString(String str) {
        byte[] byteArray;
        try {//from   w w w  .j av a2 s  .c o  m
            byteArray = str.getBytes("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            byteArray = str.getBytes();
        }
        return bytesToHexString(byteArray);
    }

    public static String bytesToHexString(byte[] bytes) {
        StringBuilder sb = new StringBuilder("");

        if (bytes == null || bytes.length <= 0) {
            return null;
        }

        for (int i = 0; i < bytes.length; i++) {
            int v = bytes[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                sb.append(0);
            }
            sb.append(hv);
        }

        return sb.toString();
    }
}

Related

  1. toHex(String txt)
  2. toHex(byte[] buf)
  3. hexStr2Str(String hexStr)
  4. hexStringToCommonString(String hexString)
  5. toHex(String txt)
  6. toHex(String txt)