MD5轉換成數字
阿新 • • 發佈:2021-10-08
如題,uuid生成32位字串,想把這個字串 變成數字
/** * md5轉數值型 * 這個演算法返回值理想長度是16,因為md.length = 16 */ public static String ConvertNum(String s) { if(StringUtil.isEmpty(s)){ return null; } // 這裡根據MD5時間的值更換16進位制abc的大小寫 char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; try { byte[] btInput = s.getBytes(); //獲得MD5摘要演算法的 MessageDigest 物件 MessageDigest mdInst = MessageDigest.getInstance("MD5"); //使用指定的位元組更新摘要 mdInst.update(btInput); //獲得密文byte[] md = mdInst.digest(); char str[] = new char[md.length]; int k = 0; for (int i = 0; i < md.length; i++) { byte byte0 = md[i]; //只取高位 str[k++] = hexDigits[(byte0 >>> 4 & 0xf) % 10]; }return new String(str); } catch (Exception e) { e.printStackTrace(); return null; } }