工作上有这样的需求之前找了好多都不行,好不容易有个可以的赶紧留下来。
原址:http://blog.163.com/roadwalker@126/blog/static/113561841201013525720/
字符串转换成十六进制字符串方法1: /**
* 字符串转换成十六进制字符串
*/
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 < bs.length; i++) {
bit = (bs[i] & 0x0f0) >> 4;
sb.append(chars[bit]);
bit = bs[i] & 0x0f;
sb.append(chars[bit]);
}
return sb.toString();
} 十六进制字符串转换成为数组方法1: /**
* 把16进制字符串转换成字节数组
* @param hexString
* @return byte[]
*/
public static byte[] hexStringToByte(String hex) {
int len = (hex.length() / 2);
byte[] result = new byte[len];
char[] achar = hex.toCharArray();
for (int i = 0; i < len; i++) {
int pos = i * 2;
result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
}
return result;
} private static int toByte(char c) {
byte b = (byte) "0123456789ABCDEF".indexOf(c);
return b;
} 数组转换成十六进制字符串方法1: /**
* 数组转换成十六进制字符串
* @param byte[]
* @return HexString
*/
public static final String bytesToHexString(byte[] bArray) {
StringBuffer sb = new StringBuffer(bArray.length);
String sTemp;
for (int i = 0; i < bArray.length; i++) {
sTemp = Integer.toHexString(0xFF & bArray[i]);
if (sTemp.length() < 2)
sb.append(0);
sb.append(sTemp.toUpperCase());
}
return sb.toString();
} byte[]数组转换成十六进制字符串方法2: /**
* 数组转成十六进制字符串
* @param byte[]
* @return HexString
*/
public static String toHexString1(byte[] b){
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < b.length; ++i){
buffer.append(toHexString1(b[i]));
}
return buffer.toString();
}
public static String toHexString1(byte b){
String s = Integer.toHexString(b & 0xFF);
if (s.length() == 1){
return "0" + s;
}else{
return s;
}
} 十六进制字符串转换字符串方法1: /**
* 十六进制字符串转换成字符串
* @param hexString
* @return String
*/
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 < bytes.length; i++) {
n = str.indexOf(hexs[2 * i]) * 16;
n += str.indexOf(hexs[2 * i + 1]);
bytes[i] = (byte) (n & 0xff);
}
return new String(bytes);
} 十六进制字符串转换字符串方法2: /**
* 十六进制字符串转换字符串
* @param HexString
* @return String
*/
public static String toStringHex(String s) {
byte[] baKeyword = new byte[s.length() / 2];
for (int i = 0; i < baKeyword.length; i++) {
try {
baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(
i * 2, i * 2 + 2), 16));
} catch (Exception e) {
e.printStackTrace();
}
}
try {
s = new String(baKeyword, "utf-8");// UTF-16le:Not
} catch (Exception e1) {
e1.printStackTrace();
}
return s;
}