public static void main(String args[]) throws NoSuchAlgorithmException { String s = new String("dsajgbqignbopuadhbgnhpjaunaob"); MessageDigest md5 = MessageDigest.getInstance("MD5"); md5.update(s.getBytes()); System.out.println(bytes2Hex(md5.digest())); } private static String bytes2Hex(byte[] bts) { String des = ""; String tmp = null; for (int i = 0; i < bts.length; i++) { tmp = (Integer.toHexString(bts[i] & 0xFF)); if (tmp.length() == 1) { des += "0"; } des += tmp; } return des; }
另一种2进制转16进制的方式
private static String byteToHex(final byte[] hash) { Formatter formatter = new Formatter(); for (byte b : hash) { formatter.format("%02x", b); } String result = formatter.toString(); formatter.close(); return result; }