该工具类作用是字节,字符数组的相互转换(避免重复造轮子),分享给大家
public class ConvertUtil {
/**
* 字节数组转16进制字符串
*/
public static String bytes2HexString(byte[] b) {
String r = "";
for (int i = 0; i < b.length; i++) {
String hex = Integer.toHexString(b[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
r += hex.toUpperCase();
}
return r;
}
/**
* 字符转换为字节
*/
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
/**
* 16进制字符串转字节数组
*/
public static byte[] hexString2Bytes(String hex) {
if (hex == null || hex.equals("")) {
return null;
} else if (hex.length() % 2 != 0) {
return null;
} else {
hex = hex.toUpperCase();
int len = hex.length() / 2;
byte[] b = new byte[len];
char[] hc = hex.toCharArray();
for (int i = 0; i < len; i++) {
int p = 2 * i;
b[i] = (byte) (charToByte(hc[p]) << 4 | charToByte(hc[p + 1]));
}
return b;
}
}
/**
* short到字节数组的转换.
*/
public static byte[] short2Byte(short number) {
int temp = number;
byte[] b = new byte[2];
for (int i = 0; i < b.length; i++) {
b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位
temp = temp >> 8;// 向右移8位
}
return b;
}
/**
* 字节数组到short的转换.
*/
public static short byte2Short(byte[] b) {
short s = 0;
short s0 = (short) (b[0] & 0xff);// 最低位
short s1 = (short) (b[1] & 0xff);
s1 <<= 8;
s = (short) (s0 | s1);
return s;
}
/**
* int到字节数组的转换.
*/
public static byte[] int2Byte(int number) {
int temp = number;
byte[] b = new byte[4];
for (int i = 0; i < b.length; i++) {
b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位
temp = temp >> 8;// 向右移8位
}
return b;
}
/**
* 字节数组到int的转换.
*/
public static int byte2Int(byte[] b) {
int s = 0;
int s0 = b[0] & 0xff;// 最低位
int s1 = b[1] & 0xff;
int s2 = b[2] & 0xff;
int s3 = b[3] & 0xff;
s3 <<= 24;
s2 <<= 16;
s1 <<= 8;
s = s0 | s1 | s2 | s3;
return s;
}
/**
* long类型转成byte数组
*/
public static byte[] long2Byte(long number) {
long temp = number;
byte[] b = new byte[8];
for (int i = 0; i < b.length; i++) {
b[i] = new Long(temp & 0xff).byteValue();// 将最低位保存在最低位 temp = temp
// >> 8;// 向右移8位
}
return b;
}
/**
* 字节数组到long的转换.
*/
public static long byte2Long(byte[] b) {
long s = 0;
long s0 = b[0] & 0xff;// 最低位
long s1 = b[1] & 0xff;
long s2 = b[2] & 0xff;
long s3 = b[3] & 0xff;
long s4 = b[4] & 0xff;// 最低位
long s5 = b[5] & 0xff;
long s6 = b[6] & 0xff;
long s7 = b[7] & 0xff;
// s0不变
s1 <<= 8;
s2 <<= 16;
s3 <<= 24;
s4 <<= 8 * 4;
s5 <<= 8 * 5;
s6 <<= 8 * 6;
s7 <<= 8 * 7;
s = s0 | s1 | s2 | s3 | s4 | s5 | s6 | s7;
return s;
}
/**
* double到字节数组的转换.
*/
public static byte[] double2Byte(double num) {
byte[] b = new byte[8];
long l = Double.doubleToLongBits(num);
for (int i = 0; i < 8; i++) {
b[i] = new Long(l).byteValue();
l = l >> 8;
}
return b;
}
/**
* 字节数组到double的转换.
*/
public static double byte2Double(byte[] b) {
long m;
m = b[0];
m &= 0xff;
m |= (long) b[1] << 8;
m &= 0xffff;
m |= (long) b[2] << 16;
m &= 0xffffff;
m |= (long) b[3] << 24;
m &= 0xffffffffl;
m |= (long) b[4] << 32;
m &= 0xffffffffffl;
m |= (long) b[5] << 40;
m &= 0xffffffffffffl;
m |= (long) b[6] << 48;
m &= 0xffffffffffffffl;
m |= (long) b[7] << 56;
return Double.longBitsToDouble(m);
}
/**
* float到字节数组的转换.
*/
public static void float2Byte(float x) {
//先用 Float.floatToIntBits(f)转换成int
}
/**
* 字节数组到float的转换.
*/
public static float byte2Float(byte[] b) {
// 4 bytes
int accum = 0;
for (int shiftBy = 0; shiftBy < 4; shiftBy++) {
accum |= (b[shiftBy] & 0xff) << shiftBy * 8;
}
return Float.intBitsToFloat(accum);
}
/**
* char到字节数组的转换.
*/
public static byte[] char2Byte(char c) {
byte[] b = new byte[2];
b[0] = (byte) ((c & 0xFF00) >> 8);
b[1] = (byte) (c & 0xFF);
return b;
}
/**
* 字节数组到char的转换.
*/
public static char byte2Char(byte[] b) {
char c = (char) ((b[0] & 0xFF) << 8 | b[1] & 0xFF);
return c;
}
/**
* string到字节数组的转换.
*/
public static byte[] string2Byte(String str) throws UnsupportedEncodingException {
return str.getBytes("UTF-8");
}
/**
* 字节数组到String的转换.
*/
public static String bytes2String(byte[] str) {
String keyword = null;
try {
keyword = new String(str, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return keyword;
}