世上最全最好用的数据转换工具工具类
import java.math.BigInteger;
import java.nio.charset.Charset;
public class FormatByteUtil {
public static final String GBK = "GBK";
public static final String UTF8 = "utf-8";
public static final char[] ascii = "0123456789ABCDEF".toCharArray();
private static char[] HEX_VOCABLE = {'0', '1', '2', '3', '4', '5', '6',
'7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
/**
* 将short整型数值转换为字节数组(10进制)
*
* @param data
* @return
*/
public static byte[] getBytes(short data) {
byte[] bytes = new byte[2];
bytes[0] = (byte) ((data & 0xff00) >> 8);
bytes[1] = (byte) (data & 0xff);
return bytes;
}
/**
* 将字符转换为字节数组(10进制)
*
* @param data
* @return
*/
public static byte[] getBytes(char data) {
byte[] bytes = new byte[2];
bytes[0] = (byte) (data >> 8);
bytes[1] = (byte) (data);
return bytes;
}
/**
* 将布尔值转换为字节数组(10进制)
*
* @param data
* @return
*/
public static byte[] getBytes(boolean data) {
byte[] bytes = new byte[1];
bytes[0] = (byte) (data ? 1 : 0);
return bytes;
}
/**
* 将整型数值转换为字节数组(10进制)
*
* @param data
* @return
*/
public static byte[] getBytes(int data) {
byte[] bytes = new byte[4];
bytes[0] = (byte) ((data & 0xff000000) >> 24);
bytes[1] = (byte) ((data & 0xff0000) >> 16);
bytes[2] = (byte) ((data & 0xff00) >> 8);
bytes[3] = (byte) (data & 0xff);
return bytes;
}
/**
* 将long整型数值转换为字节数组(10进制)
*
* @param data
* @return
*/
public static byte[] getBytes(long data) {
byte[] bytes = new byte[8];
bytes[0] = (byte) ((data >> 56) & 0xff);
bytes[1] = (byte) ((data >> 48) & 0xff);
bytes[2] = (byte) ((data >> 40) & 0xff);
bytes[3] = (byte) ((data >> 32) & 0xff);
bytes[4] = (byte) ((data >> 24) & 0xff);
bytes[5] = (byte) ((data >> 16) & 0xff);
bytes[6] = (byte) ((data >> 8) & 0xff);
bytes[7] = (byte) (data & 0xff);
return bytes;
}
/**
* 将float型数值转换为字节数组(10进制)
*
* @param data
* @return
*/
public static byte[] getBytes(float data) {
int intBits = Float.floatToIntBits(data);
return getBytes(intBits);
}
/**
* 将double型数值转换为字节数组(10进制)
*
* @param data
* @return
*/
public static byte[] getBytes(double data) {
long intBits = Double.doubleToLongBits(data);
return getBytes(intBits);
}
/**
* 将字符串按照charsetName编码格式的字节数组(10进制)
*
* @param data 字符串
* @param charsetName 编码格式
* @return
*/
public static byte[] getBytes(String data, String charsetName) {
Charset charset = Charset.forName(charsetName);
return data.getBytes(charset);
}
/**
* 将字符串按照GBK编码格式的字节数组(10进制)
*
* @param data
* @return
*/
public static byte[] getBytes(String data) {
return getBytes(data, GBK);
}
/**
* 将字节数组第0字节转换为布尔值(10进制)
*
* @param bytes
* @return
*/
public static boolean getBoolean(byte[] bytes) {
return bytes[0] == 1;
}
/**
* 将字节数组的第index字节转换为布尔值(10进制)
*
* @param bytes
* @param index
* @return
*/
public static boolean getBoolean(byte[] bytes, int index) {
return bytes[index] == 1;
}
/**
* 将字节数组前2字节转换为short整型数值(10进制)
*
* @param bytes
* @return
*/
public static short getShort(byte[] bytes) {
return (short) ((0xff00 & (bytes[0] << 8)) | (0xff & bytes[1]));
}
/**
* 将字节数组从startIndex开始的2个字节转换为short整型数值(10进制)
*
* @param bytes
* @param startIndex
* @return
*/
public static short getShort(byte[] bytes, int startIndex) {
return (short) ((0xff00 & (bytes[startIndex] << 8)) | (0xff & bytes[startIndex + 1]));
}
/**
* 将字节数组前2字节转换为字符(10进制)
*
* @param bytes
* @return
*/
public static char getChar(byte[] bytes) {
return (char) ((0xff00 & (bytes[0] << 8)) | (0xff & bytes[1]));
}
/**
* 将字节数组从startIndex开始的2个字节转换为字符(10进制)
*
* @param bytes
* @param startIndex
* @return
*/
public static char getChar(byte[] bytes, int startIndex) {
return (char) ((0xff00 & (bytes[startIndex] << 8)) | (0xff & bytes[startIndex + 1]));
}
/**
* 将字节数组前4字节转换为整型数值(10进制)
*
* @param bytes
* @return
*/
public static int getInt(byte[] bytes) {
bytes = formatByteArray(bytes, 4);
return (0xff000000 & (bytes[0] << 24) | (0xff0000 & (bytes[1] << 16))
| (0xff00 & (bytes[2] << 8)) | (0xff & bytes[3]));
}
/**
* 将字节数组从startIndex开始的4个字节转换为整型数值(10进制)
*
* @param bytes
* @param startIndex
* @return
*/
public static int getInt(byte[] bytes, int startIndex) {
return (0xff000000 & (bytes[startIndex] << 24)
| (0xff0000 & (bytes[startIndex + 1] << 16))
| (0xff00 & (bytes[startIndex + 2] << 8)) | (0xff & bytes[startIndex + 3]));
}
/**
* 将字节数组前8字节转换为long整型数值(10进制)
*
* @param bytes
* @return
*/
public static long getLong(byte[] bytes) {
return (0xff00000000000000L & ((long) bytes[0] << 56)
| (0xff000000000000L & ((long) bytes[1] << 48))
| (0xff0000000000L & ((long) bytes[2] << 40))
| (0xff00000000L & ((long) bytes[3] << 32))
| (0xff000000L & ((long) bytes[4] << 24))
| (0xff0000L & ((long) bytes[5] << 16))
| (0xff00L & ((long) bytes[6] << 8)) | (0xffL & (long) bytes[7]));
}
/**
* 将字节数组从startIndex开始的8个字节转换为long整型数值(10进制)
*
* @param bytes
* @param startIndex
* @return
*/
public static long getLong(byte[] bytes, int startIndex) {
return (0xff00000000000000L & ((long) bytes[startIndex] << 56)
| (0xff000000000000L & ((long) bytes[startIndex + 1] << 48))
| (0xff0000000000L & ((long) bytes[startIndex + 2] << 40))
| (0xff00000000L & ((long) bytes[startIndex + 3] << 32))
| (0xff000000L & ((long) bytes[startIndex + 4] << 24))
| (0xff0000L & ((long) bytes[startIndex + 5] << 16))
| (0xff00L & ((long) bytes[startIndex + 6] << 8)) | (0xffL & (long) bytes[startIndex + 7]));
}
/**
* 将字节数组前4字节转换为float型数值(10进制)
*
* @param bytes
* @return
*/
public static float getFloat(byte[] bytes) {
return Float.intBitsToFloat(getInt(bytes));
}
/**
* 将字节数组从startIndex开始的4个字节转换为float型数值(10进制)
*
* @param bytes
* @param startIndex
* @return
*/
public static float getFloat(byte[] bytes, int startIndex) {
byte[] result = new byte[4];
System.arraycopy(bytes, startIndex, result, 0, 4);
return Float.intBitsToFloat(getInt(result));
}
/**
* 将字节数组前8字节转换为double型数值(10进制)
*
* @param bytes
* @return
*/
public static double getDouble(byte[] bytes) {
long l = getLong(bytes);
return Double.longBitsToDouble(l);
}
/**
* 将字节数组从startIndex开始的8个字节转换为double型数值(10进制)
*
* @param bytes
* @param startIndex
* @return
*/
public static double getDouble(byte[] bytes, int startIndex) {
byte[] result = new byte[8];
System.arraycopy(bytes, startIndex, result, 0, 8);
long l = getLong(result);
return Double.longBitsToDouble(l);
}
/**
* 将charsetName编码格式的字节数组转换为字符串(10进制)
*
* @param bytes
* @param charsetName
* @return
*/
public static String getString(byte[] bytes, String charsetName) {
return new String(bytes, Charset.forName(charsetName));
}
/**
* 将UTF8编码格式的字节数组转换为字符串(10进制)
*
* @param bytes
* @return
*/
public static String getString(byte[] bytes) {
return getString(bytes, UTF8);
}
/**
* 将16进制字符串转换为字节数组使用
*
* @param c
* @return
*/
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
/**
* 字节数组转16进制字符串
*
* @param bs
* @return
*/
public static String bytesToHex(byte[] bs) {
StringBuilder sb = new StringBuilder();
for (byte b : bs) {
int high = (b >> 4) & 0x0f;
int low = b & 0x0f;
sb.append(HEX_VOCABLE[high]);
sb.append(HEX_VOCABLE[low]);
}
return sb.toString();
}
/**
* 字节数组取前len个字节转16进制字符串
*
* @param bs
* @param len
* @return
*/
public static String bytesToHex(byte[] bs, int len) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < len; i++) {
byte b = bs[i];
int high = (b >> 4) & 0x0f;
int low = b & 0x0f;
sb.append(HEX_VOCABLE[high]);
sb.append(HEX_VOCABLE[low]);
}
return sb.toString();
}
/**
* 字节数组偏移offset长度之后的取len个字节转16进制字符串
*
* @param bs
* @param offset
* @param len
* @return
*/
public static String bytesToHex(byte[] bs, int offset, int len) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < len; i++) {
byte b = bs[offset + i];
int high = (b >> 4) & 0x0f;
int low = b & 0x0f;
sb.append(HEX_VOCABLE[high]);
sb.append(HEX_VOCABLE[low]);
}
return sb.toString();
}
/**
* 字节转整形
*
* @param value
* @return
*/
public static int bcdToInt(byte value) {
return ((value >> 4) * 10) + (value & 0x0F);
}
/**
* 将字节数组取反
*
* @param src
* @return
*/
public static String negate(byte[] src) {
if (src == null || src.length == 0) {
return null;
}
byte[] temp = new byte[2 * src.length];
for (int i = 0; i < src.length; i++) {
byte tmp = (byte) (0xFF ^ src[i]);
temp[i * 2] = (byte) ((tmp >> 4) & 0x0f);
temp[i * 2 + 1] = (byte) (tmp & 0x0f);
}
StringBuffer res = new StringBuffer();
for (int i = 0; i < temp.length; i++) {
res.append(ascii[temp[i]]);
}
return res.toString();
}
/**
* 比较字节数组是否相同
*
* @param a
* @param b
* @return
*/
public static boolean compareBytes(byte[] a, byte[] b) {
if (a == null || a.length == 0 || b == null || b.length == 0
|| a.length != b.length) {
return false;
}
if (a.length == b.length) {
for (int i = 0; i < a.length; i++) {
if (a[i] != b[i]) {
return false;
}
}
} else {
return false;
}
return true;
}
/**
* 只比对指定长度byte
*
* @param a
* @param b
* @param len
* @return
*/
public static boolean compareBytes(byte[] a, byte[] b, int len) {
if (a == null || a.length == 0 || b == null || b.length == 0
|| a.length < len || b.length < len) {
return false;
}
for (int i = 0; i < len; i++) {
if (a[i] != b[i]) {
return false;
}
}
return true;
}
/**
* 将字节数组转换为二进制字符串
*
* @param items
* @return
*/
public static String bytesToBinaryString(byte[] items) {
if (items == null || items.length == 0) {
return null;
}
StringBuffer buf = new StringBuffer();
for (byte item : items) {
buf.append(byteToBinaryString(item));
}
return buf.toString();
}
/**
* 将字节转换为二进制字符串
*
* @param item
* @return
*/
public static String byteToBinaryString(byte item) {
byte a = item;
StringBuffer buf = new StringBuffer();
for (int i = 0; i < 8; i++) {
buf.insert(0, a % 2);
a = (byte) (a >> 1);
}
return buf.toString();
}
/**
* 将short整型数值转换为字节数组
*
* @param num
* @return
*/
public static byte[] shortToBytes(short num) {
byte[] temp = new byte[2];
for (int i = 0; i < 2; i++) {
temp[i] = (byte) ((num >>> (8 - i * 8)) & 0xFF);
}
return temp;
}
/**
* 将字节数组转为整型
*
* @param arr
* @return
*/
public static int bytesToShort(byte[] arr) {
int mask = 0xFF;
int temp = 0;
int result = 0;
for (int i = 0; i < 2; i++) {
result <<= 8;
temp = arr[i] & mask;
result |= temp;
}
return result;
}
/**
* 将整型数值转换为指定长度的字节数组
*
* @param num
* @return
*/
public static byte[] intToBytes(int num) {
byte[] temp = new byte[4];
for (int i = 0; i < 4; i++) {
temp[i] = (byte) ((num >>> (24 - i * 8)) & 0xFF);
}
return temp;
}
/**
* 将整型数值转换为指定长度的字节数组
*
* @param src
* @param len
* @return
*/
public static byte[] intToBytes(int src, int len) {
if (len < 1 || len > 4) {
return null;
}
byte[] temp = new byte[len];
for (int i = 0; i < len; i++) {
temp[len - 1 - i] = (byte) ((src >>> (8 * i)) & 0xFF);
}
return temp;
}
/**
* 将字节数组转换为整型数值
*
* @param arr
* @return
*/
public static int bytesToInt(byte[] arr) {
int mask = 0xFF;
int temp = 0;
int result = 0;
for (int i = 0; i < 4; i++) {
result <<= 8;
temp = arr[i] & mask;
result |= temp;
}
return result;
}
/**
* 将long整型数值转换为字节数组
*
* @param num
* @return
*/
public static byte[] longToBytes(long num) {
byte[] temp = new byte[8];
for (int i = 0; i < 8; i++) {
temp[i] = (byte) ((num >>> (56 - i * 8)) & 0xFF);
}
return temp;
}
/**
* 将字节数组转换为long整型数值
*
* @param arr
* @return
*/
public static long bytesToLong(byte[] arr) {
int mask = 0xFF;
int temp = 0;
long result = 0;
int len = Math.min(8, arr.length);
for (int i = 0; i < len; i++) {
result <<= 8;
temp = arr[i] & mask;
result |= temp;
}
return result;
}
/**
* 将16进制字符转换为字节
*
* @param c
* @return
*/
public static byte toByte(char c) {
byte b = (byte) "0123456789ABCDEF".indexOf(c);
return b;
}
/**
* 功能描述:把两个字节的字节数组转化为整型数据,高位补零,
* 有字节数组byte[] data = new byte[]{1,2};转换后int数据的字节分布如下:
* 00000000 00000000 00000001 00000010,函数返回258
*
* @param lenData 需要进行转换的字节数组
* @return 字节数组所表示整型值的大小
*/
public static int bytesToIntWhereByteLengthEquals2(byte lenData[]) {
if (lenData.length != 2) {
return -1;
}
byte fill[] = new byte[]{0, 0};
byte real[] = new byte[4];
System.arraycopy(fill, 0, real, 0, 2);
System.arraycopy(lenData, 0, real, 2, 2);
int len = byteToInt(real);
return len;
}
/**
* 功能描述:将byte数组转化为int类型的数据
*
* @param byteVal 需要转化的字节数组
* @return 字节数组所表示的整型数据
*/
public static int byteToInt(byte[] byteVal) {
int result = 0;
for (int i = 0; i < byteVal.length; i++) {
int tmpVal = (byteVal[i] << (8 * (3 - i)));
switch (i) {
case 0:
tmpVal = tmpVal & 0xFF000000;
break;
case 1:
tmpVal = tmpVal & 0x00FF0000;
break;
case 2:
tmpVal = tmpVal & 0x0000FF00;
break;
case 3:
tmpVal = tmpVal & 0x000000FF;
break;
}
result = result | tmpVal;
}
return result;
}
public static byte CheckXORSum(byte[] bData) {
byte sum = 0x00;
for (int i = 0; i < bData.length; i++) {
sum ^= bData[i];
}
return sum;
}
/**
* 从offset开始 将后续长度为len的byte字节转为int
*
* @param data
* @param offset
* @param len
* @return
*/
public static int bytesToInt(byte[] data, int offset, int len) {
int mask = 0xFF;
int temp = 0;
int result = 0;
len = Math.min(len, 4);
for (int i = 0; i < len; i++) {
result <<= 8;
temp = data[offset + i] & mask;
result |= temp;
}
return result;
}
/**
* byte字节数组中的字符串的长度
*
* @param data
* @return
*/
public static int getBytesStringLen(byte[] data) {
int count = 0;
for (byte b : data) {
if (b == 0x00) {
break;
}
count++;
}
return count;
}
/**
* byte数组补位
*/
public static byte[] formatByteArray(byte[] bytes, int length) {
int bytelength = length - bytes.length;
int baseByteLenth = bytelength;
if (bytelength > 0) {
int index = 0;
byte[] result = new byte[length];
while (bytelength > 0) {
result[index] = 0;
index++;
bytelength--;
}
for (int i = index; i < length; i++) {
result[i] = bytes[i - baseByteLenth];
}
return result;
} else {
return bytes;
}
}
/**
* byte数组转int数组
*
* @param btarr
* @return
*/
public static int[] byteArrayToIntArray(byte[] btarr) {
if (btarr.length % 4 != 0) {
return null;
}
int[] intarr = new int[btarr.length / 4];
int i1, i2, i3, i4;
for (int j = 0, k = 0; j < intarr.length; j++, k += 4)//j循环int k循环byte数组
{
i1 = btarr[k];
i2 = btarr[k + 1];
i3 = btarr[k + 2];
i4 = btarr[k + 3];
if (i1 < 0) {
i1 += 256;
}
if (i2 < 0) {
i2 += 256;
}
if (i3 < 0) {
i3 += 256;
}
if (i4 < 0) {
i4 += 256;
}
intarr[j] = (i1 << 24) + (i2 << 16) + (i3 << 8) + (i4 << 0);
}
return intarr;
}
/**
* byte数组转int数组
*
* @param btarr
* @return
*/
public static long[] byteArrayToUintArray(byte[] btarr) {
if (btarr.length % 4 != 0) {
return null;
}
long[] intarr = new long[btarr.length / 4];
int i1, i2, i3, i4;
for (int j = 0, k = 0; j < intarr.length; j++, k += 4)//j循环int k循环byte数组
{
i1 = btarr[k];
i2 = btarr[k + 1];
i3 = btarr[k + 2];
i4 = btarr[k + 3];
if (i1 < 0) {
i1 += 256;
}
if (i2 < 0) {
i2 += 256;
}
if (i3 < 0) {
i3 += 256;
}
if (i4 < 0) {
i4 += 256;
}
intarr[j] = ((i1 << 24) + (i2 << 16) + (i3 << 8) + (i4 << 0)) & 0xFFFFFFFFL;
}
return intarr;
}
/**
* int数组转byte数组
*
* @param intarr
* @return
*/
public static byte[] intArrayToByteArray(int[] intarr) {
int bytelength = intarr.length * 4;//长度
byte[] bt = new byte[bytelength];//开辟数组
int curint = 0;
for (int j = 0, k = 0; j < intarr.length; j++, k += 4) {
curint = intarr[j];
bt[k] = (byte) ((curint >> 24) & 0b1111_1111);//右移4位,与1作与运算
bt[k + 1] = (byte) ((curint >> 16) & 0b1111_1111);
bt[k + 2] = (byte) ((curint >> 8) & 0b1111_1111);
bt[k + 3] = (byte) ((curint >> 0) & 0b1111_1111);
}
return bt;
}
/**
* 10进制转16进制(byte)
*
* @param value
* @return
*/
public static byte[] toHexByte(Byte value) {
return formatByteArray(getBytesByHex(byteToHex(value)), 1);
}
/**
* 10进制转16进制(short)
*
* @param value
* @return
*/
public static byte[] toHexByte(Short value) {
return formatByteArray(getBytesByHex(shortToHex(value)), 2);
}
/**
* 10进制转16进制(int)
*
* @param value
* @return
*/
public static byte[] toHexByte(Integer value) {
return formatByteArray(getBytesByHex(Integer.toHexString(value)), 4);
}
/**
* 10进制转16进制(value)
*
* @param value
* @return
*/
public static byte[] toHexByte(Long value) {
return formatByteArray(getBytesByHex(Long.toHexString(value)), 8);
}
/**
* 10进制字符串转16进制数组
*
* @param value
* @param byteArrayLength
* @return
*/
public static byte[] toHexByte(String value, int byteArrayLength) {
return formatByteArray(getBytesByHex(strToHex(value)), byteArrayLength);
}
/**
* 字符串转换成为16进制(无需Unicode编码)
*
* @param value
* @return
*/
public static String strToHex(String value) {
char[] chars = "0123456789ABCDEF".toCharArray();
StringBuilder sb = new StringBuilder("");
byte[] bs = value.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().trim();
}
/**
* 字节转16进制字符串
*
* @param b
* @return
*/
public static String byteToHex(byte b) {
StringBuilder sb = new StringBuilder();
int high = (b >> 4) & 0x0f;
int low = b & 0x0f;
sb.append(HEX_VOCABLE[high]);
sb.append(HEX_VOCABLE[low]);
return sb.toString();
}
/**
* short转16进制字符串
*
* @param b
* @return
*/
public static String shortToHex(short b) {
return bytesToHex(shortToBytes(b));
}
/**
* int转换成为16进制
*
* @param value
* @return
*/
public static String strToHex(int value) {
return Integer.toHexString(value);
}
/**
* long转换成为16进制
*
* @param value
* @return
*/
public static String strToHex(long value) {
return Long.toHexString(value);
}
/**
* 字节数组循环拼接string
*
* @return
*/
public static String byteToString(byte[] byteValue) {
String value = "";
for (int i = 0; i < byteValue.length; i++) {
value += byteValue[i];
}
return value;
}
/**
* string字符串循环byte[]
*
* @param strValue
* @return
*/
public static byte[] stringToByte(String strValue) {
byte[] value = new byte[strValue.length()];
for (int i = 0; i < strValue.length(); i++) {
value[i] = Byte.valueOf(String.valueOf(strValue.charAt(i)));
}
return value;
}
/**
* 字节数组转成16进制表示格式的字符串
*
* @param byteArray 需要转换的字节数组
* @return 16进制表示格式的字符串
*/
public static String toHexString(byte[] byteArray) {
if (byteArray == null || byteArray.length < 1)
throw new IllegalArgumentException("this byteArray must not be null or empty");
final StringBuilder hexString = new StringBuilder();
for (int i = 0; i < byteArray.length; i++) {
if ((byteArray[i] & 0xff) < 0x10) // 0~F前面不零
hexString.append("0");
hexString.append(Integer.toHexString(0xFF & byteArray[i]));
}
return hexString.toString().toLowerCase();
}
/**
* 将16进制字符串转换为字节数组
*
* @param hex
* @return
*/
public static byte[] getBytesByHex(String hex) {
if (hex == null || hex.equals("")) {
return null;
}
hex = hex.toUpperCase();
int length = hex.length() / 2;
char[] hexChars = hex.toCharArray();
byte[] d;
//转换的时候应该注意的是单双数的情况,网上很多都只处理了双数的情况,也就是默认刚好填满,这样16进制字符串长度是单数的话会丢失掉一个字符 因为length/2 是舍去余数的
if (hex.length() % 2 != 0) {// 16进制字符串长度是单数
length = length + 1;
d = new byte[length];
// 这里把byte数组从后往前填,字符串也是翻转着填的,最后会空出byte数组的第一个(用来填充我们单出来的那个字符)
for (int i = length - 1; i > 0; i--) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) | charToByte(hexChars[pos - 1]) << 4);
}
d[0] = charToByte(hexChars[0]);
} else {// 双数情况
d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
}
return d;
}
}