mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-11 16:18:02 +08:00
feat: 修复ByteUtil工具类大小端转换问题
修复ByteUtil工具类大小端转换问题
This commit is contained in:
parent
fca211de07
commit
d4a7ddac3b
@ -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);
|
||||||
}
|
}
|
||||||
@ -301,6 +300,7 @@ public class ByteUtil {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 将{@link Number}转换为
|
* 将{@link Number}转换为
|
||||||
|
*
|
||||||
* @param number 数字
|
* @param number 数字
|
||||||
* @return bytes
|
* @return bytes
|
||||||
*/
|
*/
|
||||||
@ -310,6 +310,7 @@ public class ByteUtil {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 将{@link Number}转换为
|
* 将{@link Number}转换为
|
||||||
|
*
|
||||||
* @param number 数字
|
* @param number 数字
|
||||||
* @param byteOrder 端序
|
* @param byteOrder 端序
|
||||||
* @return bytes
|
* @return bytes
|
||||||
|
Loading…
Reference in New Issue
Block a user