源代码下载地址: http://www.zuidaima.com/share/1550463378410496.htm
Java 字符串转 16 进制工具类 Hex.java 实现 16进制 0xfecd .. 和 java 字符串之间的互转换! 如果做开发,通常用户登陆密码都会 mad5(salt + pwd) 然后再将 md 之后的数据 hex 一下。 这个工具类,就是实现此效果的。
package com.zuidaima.haohui.common.utils;
public class Hex {public static String str2HexStr(String str) {char[] chars = "0123456789ABCDEF".toCharArray();StringBuilder sb = new StringBuilder("");byte[] bs = str.getBytes();int bit;for (int i = 0; i 0x0f0) >> 4;sb.append(chars[bit]);bit = bs[i] & 0x0f;sb.append(chars[bit]);sb.append(' ');}return sb.toString().trim();}public static String hexStr2Str(String hexStr) {String str = "0123456789ABCDEF";char[] hexs = hexStr.toCharArray();byte[] bytes = new byte[hexStr.length() / 2];int n;for (int i = 0; i 2 * i]) * 16;n += str.indexOf(hexs[2 * i + 1]);bytes[i] = (byte) (n & 0xff);}return new String(bytes);}public static String byte2HexStr(byte[] b) {String stmp = "";StringBuilder sb = new StringBuilder("");for (int n = 0; n 0xFF);sb.append((stmp.length() == 1) ? "0" + stmp : stmp);
}return sb.toString().toUpperCase().trim();}public static byte[] hexStr2Bytes(String src) {int m = 0, n = 0;int l = src.length() / 2;System.out.println(l);byte[] ret = new byte[l];for (int i = 0; i 2 + 1;n = m + 1;ret[i] = Byte.decode("0x" + src.substring(i * 2, m) + src.substring(m, n));}return ret;}public static String strToUnicode(String strText) throws Exception {char c;StringBuilder str = new StringBuilder();int intAsc;String strHex;for (int i = 0; i int) c;strHex = Integer.toHexString(intAsc);if (intAsc > 128)str.append("\\u" + strHex);elsestr.append("\\u00" + strHex);}return str.toString();}public static String unicodeToString(String hex) {int t = hex.length() / 6;StringBuilder str = new StringBuilder();for (int i = 0; i 6, (i + 1) * 6);String s1 = s.substring(2, 4) + "00";String s2 = s.substring(4);int n = Integer.valueOf(s1, 16) + Integer.valueOf(s2, 16);char[] chars = Character.toChars(n);str.append(new String(chars));}return str.toString();}public static void main(String[] args) {String hex = "ef2c71b29202f3e642f2abd8d518f367ec3fbf6a6a61beb678ae0c871ee368ac";System.out.println(Hex.hexStr2Str(hex));}
}