!384 修复ByteUtil工具类大小端转换问题

Merge pull request !384 from ZhouChuGang/v5-dev
This commit is contained in:
Looly 2021-07-27 17:35:54 +00:00 committed by Gitee
commit b8c07fd098

View File

@ -64,9 +64,10 @@ public class ByteUtil {
*/ */
public static short bytesToShort(byte[] bytes, ByteOrder byteOrder) { public static short bytesToShort(byte[] bytes, ByteOrder byteOrder) {
if (ByteOrder.LITTLE_ENDIAN == byteOrder) { if (ByteOrder.LITTLE_ENDIAN == byteOrder) {
return (short) (bytes[1] & 0xff | (bytes[0] & 0xff) << Byte.SIZE); //小端模式数据的高字节保存在内存的高地址中而数据的低字节保存在内存的低地址中
} else {
return (short) (bytes[0] & 0xff | (bytes[1] & 0xff) << Byte.SIZE); return (short) (bytes[0] & 0xff | (bytes[1] & 0xff) << Byte.SIZE);
} else {
return (short) (bytes[1] & 0xff | (bytes[0] & 0xff) << Byte.SIZE);
} }
} }
@ -92,11 +93,11 @@ public class ByteUtil {
public static byte[] shortToBytes(short shortValue, ByteOrder byteOrder) { public static byte[] shortToBytes(short shortValue, ByteOrder byteOrder) {
byte[] b = new byte[Short.BYTES]; byte[] b = new byte[Short.BYTES];
if (ByteOrder.LITTLE_ENDIAN == byteOrder) { if (ByteOrder.LITTLE_ENDIAN == byteOrder) {
b[1] = (byte) (shortValue & 0xff);
b[0] = (byte) ((shortValue >> Byte.SIZE) & 0xff);
} else {
b[0] = (byte) (shortValue & 0xff); b[0] = (byte) (shortValue & 0xff);
b[1] = (byte) ((shortValue >> Byte.SIZE) & 0xff); b[1] = (byte) ((shortValue >> Byte.SIZE) & 0xff);
} else {
b[1] = (byte) (shortValue & 0xff);
b[0] = (byte) ((shortValue >> Byte.SIZE) & 0xff);
} }
return b; return b;
} }
@ -122,15 +123,15 @@ public class ByteUtil {
*/ */
public static int bytesToInt(byte[] bytes, ByteOrder byteOrder) { public static int bytesToInt(byte[] bytes, ByteOrder byteOrder) {
if (ByteOrder.LITTLE_ENDIAN == byteOrder) { if (ByteOrder.LITTLE_ENDIAN == byteOrder) {
return bytes[3] & 0xFF | //
(bytes[2] & 0xFF) << 8 | //
(bytes[1] & 0xFF) << 16 | //
(bytes[0] & 0xFF) << 24; //
} else {
return bytes[0] & 0xFF | // return bytes[0] & 0xFF | //
(bytes[1] & 0xFF) << 8 | // (bytes[1] & 0xFF) << 8 | //
(bytes[2] & 0xFF) << 16 | // (bytes[2] & 0xFF) << 16 | //
(bytes[3] & 0xFF) << 24; // (bytes[3] & 0xFF) << 24; //
} else {
return bytes[3] & 0xFF | //
(bytes[2] & 0xFF) << 8 | //
(bytes[1] & 0xFF) << 16 | //
(bytes[0] & 0xFF) << 24; //
} }
} }
@ -157,22 +158,21 @@ public class ByteUtil {
public static byte[] intToBytes(int intValue, ByteOrder byteOrder) { public static byte[] intToBytes(int intValue, ByteOrder byteOrder) {
if (ByteOrder.LITTLE_ENDIAN == byteOrder) { if (ByteOrder.LITTLE_ENDIAN == byteOrder) {
return new byte[]{ //
(byte) ((intValue >> 24) & 0xFF), //
(byte) ((intValue >> 16) & 0xFF), //
(byte) ((intValue >> 8) & 0xFF), //
(byte) (intValue & 0xFF) //
};
} else {
return new byte[]{ // return new byte[]{ //
(byte) (intValue & 0xFF), // (byte) (intValue & 0xFF), //
(byte) ((intValue >> 8) & 0xFF), // (byte) ((intValue >> 8) & 0xFF), //
(byte) ((intValue >> 16) & 0xFF), // (byte) ((intValue >> 16) & 0xFF), //
(byte) ((intValue >> 24) & 0xFF) // (byte) ((intValue >> 24) & 0xFF) //
}; };
}
} else {
return new byte[]{ //
(byte) ((intValue >> 24) & 0xFF), //
(byte) ((intValue >> 16) & 0xFF), //
(byte) ((intValue >> 8) & 0xFF), //
(byte) (intValue & 0xFF) //
};
}
} }
@ -200,17 +200,16 @@ public class ByteUtil {
public static byte[] longToBytes(long longValue, ByteOrder byteOrder) { public static byte[] longToBytes(long longValue, ByteOrder byteOrder) {
byte[] result = new byte[Long.BYTES]; byte[] result = new byte[Long.BYTES];
if (ByteOrder.LITTLE_ENDIAN == byteOrder) { if (ByteOrder.LITTLE_ENDIAN == byteOrder) {
for (int i = (result.length - 1); i >= 0; i--) {
result[i] = (byte) (longValue & 0xFF);
longValue >>= Byte.SIZE;
}
} else {
for (int i = 0; i < result.length; i++) { for (int i = 0; i < result.length; i++) {
result[i] = (byte) (longValue & 0xFF); result[i] = (byte) (longValue & 0xFF);
longValue >>= Byte.SIZE; longValue >>= Byte.SIZE;
} }
} else {
for (int i = (result.length - 1); i >= 0; i--) {
result[i] = (byte) (longValue & 0xFF);
longValue >>= Byte.SIZE;
}
} }
return result; return result;
} }
@ -238,12 +237,12 @@ public class ByteUtil {
public static long bytesToLong(byte[] bytes, ByteOrder byteOrder) { public static long bytesToLong(byte[] bytes, ByteOrder byteOrder) {
long values = 0; long values = 0;
if (ByteOrder.LITTLE_ENDIAN == byteOrder) { if (ByteOrder.LITTLE_ENDIAN == byteOrder) {
for (int i = 0; i < Long.BYTES; i++) { for (int i = (Long.BYTES - 1); i >= 0; i--) {
values <<= Byte.SIZE; values <<= Byte.SIZE;
values |= (bytes[i] & 0xff); values |= (bytes[i] & 0xff);
} }
} else { } else {
for (int i = (Long.BYTES - 1); i >= 0; i--) { for (int i = 0; i < Long.BYTES; i++) {
values <<= Byte.SIZE; values <<= Byte.SIZE;
values |= (bytes[i] & 0xff); values |= (bytes[i] & 0xff);
} }
@ -269,7 +268,7 @@ public class ByteUtil {
* from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java * from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java
* *
* @param doubleValue double值 * @param doubleValue double值
* @param byteOrder 端序 * @param byteOrder 端序
* @return byte数组 * @return byte数组
*/ */
public static byte[] doubleToBytes(double doubleValue, ByteOrder byteOrder) { public static byte[] doubleToBytes(double doubleValue, ByteOrder byteOrder) {
@ -301,29 +300,31 @@ public class ByteUtil {
/** /**
* {@link Number}转换为 * {@link Number}转换为
*
* @param number 数字 * @param number 数字
* @return bytes * @return bytes
*/ */
public static byte[] numberToBytes(Number number){ public static byte[] numberToBytes(Number number) {
return numberToBytes(number, ByteOrder.LITTLE_ENDIAN); return numberToBytes(number, ByteOrder.LITTLE_ENDIAN);
} }
/** /**
* {@link Number}转换为 * {@link Number}转换为
* @param number 数字 *
* @param number 数字
* @param byteOrder 端序 * @param byteOrder 端序
* @return bytes * @return bytes
*/ */
public static byte[] numberToBytes(Number number, ByteOrder byteOrder){ public static byte[] numberToBytes(Number number, ByteOrder byteOrder) {
if(number instanceof Double){ if (number instanceof Double) {
return doubleToBytes((Double) number, byteOrder); return doubleToBytes((Double) number, byteOrder);
} else if(number instanceof Long){ } else if (number instanceof Long) {
return longToBytes((Long) number, byteOrder); return longToBytes((Long) number, byteOrder);
} else if(number instanceof Integer){ } else if (number instanceof Integer) {
return intToBytes((Integer) number, byteOrder); return intToBytes((Integer) number, byteOrder);
} else if(number instanceof Short){ } else if (number instanceof Short) {
return shortToBytes((Short) number, byteOrder); return shortToBytes((Short) number, byteOrder);
} else{ } else {
return doubleToBytes(number.doubleValue(), byteOrder); return doubleToBytes(number.doubleValue(), byteOrder);
} }
} }