Javabyte数组与⼗六进制字符串互转字符串转换成⼗六进制字符串⽅法1:
/**
* 字符串转换成⼗六进制字符串
*/
public static String str2HexStr(String str) {
char[] chars = "0123456789ABCDEF".toCharArray();
StringBuilder sb = new StringBuilder("");
byte[] bs = Bytes();
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]);
}
String();
}
⼗六进制字符串转换成为数组⽅法1:
/**
* 把16进制字符串转换成字节数组
* @param hexString
* @return byte[]
*/
public static byte[] hexStringToByte(String hex) {
int len = (hex.length() / 2);
byte[] result = new byte[len];
char[] achar = CharArray();
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 = HexString(0xFF & bArray[i]);
if (sTemp.length() < 2)
sb.append(0);
sb.UpperCase());
}
String();
}
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]));
}
String();
}
public static String toHexString1(byte b){
String s = HexString(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 = CharArray();
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;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论