Here you can find the source of toHex(String str)
public static String toHex(String str)
//package com.java2s; //License from project: Apache License public class Main { public static String toHex(String str) { String hexString = "0123456789ABCDEF"; byte[] bytes = str.getBytes(); StringBuilder sb = new StringBuilder(bytes.length * 2); for (byte aByte : bytes) { char temp1 = hexString.charAt((aByte & 0xf0) >> 4); char temp2 = hexString.charAt(aByte & 0x0f); if (!(temp1 == '0' && temp2 == '0')) { sb.append(temp1);//from w w w.j a v a 2 s . c o m sb.append(temp2); } } return sb.toString(); } }