mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-06-17 17:10:26 +08:00
ByteHelper新发现一个错误
This commit is contained in:
@@ -94,16 +94,6 @@ public class ByteHelper {
|
||||
}
|
||||
return value;
|
||||
}
|
||||
//无符号byte[2]转int
|
||||
public static int byteArrayToInt16(byte[]bytes){
|
||||
int value= 0;
|
||||
//由高位到低位
|
||||
for (int i = 0; i < 2; i++) {
|
||||
int offset= (bytes.length - 1 - i) * 8;
|
||||
value +=(bytes[i] & 0x000000ff) << offset; //往高位游
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 无符号long转byte[]
|
||||
@@ -295,4 +285,25 @@ public class ByteHelper {
|
||||
System.out.println(b&0xff);
|
||||
}
|
||||
|
||||
// 便于测试,新添加两个public方法:
|
||||
// 无符号byte[]转int
|
||||
public static int new_byteArrayToInt(byte[]bytes){
|
||||
int value= 0;
|
||||
//由高位到低位
|
||||
for (int i = 0; i < bytes.length; i++) {
|
||||
int offset= (bytes.length - 1 - i) * 8;
|
||||
value +=(bytes[i] & 0x000000ff) << offset; //往高位游
|
||||
}
|
||||
return value;
|
||||
}
|
||||
// 无符号byte[]转long
|
||||
public static long new_byteArrayToLong(byte[]bytes){
|
||||
int value= 0;
|
||||
//由高位到低位
|
||||
for (int i = 0; i < 8; i++) {
|
||||
int offset= (bytes.length - 1 - i) * 8;
|
||||
value +=(bytes[i] & 0x000000000000ff) << offset; //往高位游
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,12 +28,12 @@ public class BlockTest {
|
||||
byte[] rawBuf = block.getRaw();
|
||||
assertEquals(rawBuf[0],(byte)241);
|
||||
// rawBuf[1:3] == 241
|
||||
int a = ByteHelper.byteArrayToInt16(ByteHelper.getLenBytes(rawBuf, new VlInt(1),2));
|
||||
int a = ByteHelper.new_byteArrayToInt(ByteHelper.getLenBytes(rawBuf, new VlInt(1),2));
|
||||
assertEquals(a,241);
|
||||
//rawBuf[3] == 241
|
||||
assertEquals(rawBuf[3],(byte) 241);
|
||||
//rawBuf[4:6] == 8000
|
||||
int b = ByteHelper.byteArrayToInt16(ByteHelper.getLenBytes(rawBuf,new VlInt(4),2));
|
||||
int b = ByteHelper.new_byteArrayToInt(ByteHelper.getLenBytes(rawBuf,new VlInt(4),2));
|
||||
assertEquals(b,8000);
|
||||
//rawBuf.length == 8006
|
||||
assertEquals(rawBuf.length,8006);
|
||||
@@ -76,10 +76,10 @@ public class BlockTest {
|
||||
*/
|
||||
byte[] rawBuf = block.getRaw();
|
||||
assertEquals(rawBuf[0],(byte)241);
|
||||
int a = ByteHelper.byteArrayToInt16(ByteHelper.getLenBytes(rawBuf, new VlInt(1),2));
|
||||
int a = ByteHelper.new_byteArrayToInt(ByteHelper.getLenBytes(rawBuf, new VlInt(1),2));
|
||||
assertEquals(a,241);
|
||||
assertEquals(rawBuf[3],(byte) 241);
|
||||
int b = ByteHelper.byteArrayToInt16(ByteHelper.getLenBytes(rawBuf,new VlInt(4),2));
|
||||
int b = ByteHelper.new_byteArrayToInt(ByteHelper.getLenBytes(rawBuf,new VlInt(4),2));
|
||||
assertEquals(b,8000);
|
||||
assertEquals(rawBuf.length,8006);
|
||||
}
|
||||
@@ -101,10 +101,10 @@ public class BlockTest {
|
||||
*/
|
||||
byte[] rawBuf = block.getRaw();
|
||||
assertEquals(rawBuf[0],(byte)241);
|
||||
int a = ByteHelper.byteArrayToInt16(ByteHelper.getLenBytes(rawBuf, new VlInt(1),2));
|
||||
int a = ByteHelper.new_byteArrayToInt(ByteHelper.getLenBytes(rawBuf, new VlInt(1),2));
|
||||
assertEquals(a,241);
|
||||
assertEquals(rawBuf[3],(byte) 241);
|
||||
int b = ByteHelper.byteArrayToInt16(ByteHelper.getLenBytes(rawBuf,new VlInt(4),2));
|
||||
int b = ByteHelper.new_byteArrayToInt(ByteHelper.getLenBytes(rawBuf,new VlInt(4),2));
|
||||
assertEquals(b,8000);
|
||||
assertEquals(rawBuf.length,8006);
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ import static org.junit.Assert.*;
|
||||
assertEquals(firstbyte,test[0]);
|
||||
}
|
||||
|
||||
//error
|
||||
|
||||
@Test
|
||||
public void getLenBytes() {
|
||||
byte[] buffer = new byte[]{0x00,0x00,0x01, (byte) 0xff}; //255
|
||||
@@ -306,7 +306,7 @@ import static org.junit.Assert.*;
|
||||
assertEquals(a9,65535);
|
||||
}
|
||||
|
||||
//error
|
||||
|
||||
@Test
|
||||
public void uint16ToByteArray() {
|
||||
ByteHelper byteHelper = new ByteHelper();
|
||||
@@ -376,7 +376,7 @@ import static org.junit.Assert.*;
|
||||
assertEquals(a9,65535);
|
||||
}
|
||||
|
||||
//error
|
||||
|
||||
@Test
|
||||
public void uint32ToLong() {
|
||||
byte[] b1 = new byte[]{0x00,0x00,0x00,0x00}; //0
|
||||
@@ -423,7 +423,7 @@ import static org.junit.Assert.*;
|
||||
|
||||
}
|
||||
|
||||
//error
|
||||
|
||||
@Test
|
||||
public void uint32ToByteArray() {
|
||||
byte[] b1 = new byte[]{0x00,0x00,0x00,0x00}; //0
|
||||
@@ -472,7 +472,7 @@ import static org.junit.Assert.*;
|
||||
assertArrayEquals(a13,b13);
|
||||
}
|
||||
|
||||
//error
|
||||
|
||||
//todo 大于2^64次方无法表示
|
||||
@Test
|
||||
public void uint64ToLong() {
|
||||
@@ -493,7 +493,11 @@ import static org.junit.Assert.*;
|
||||
byte[] b15 = new byte[]{0x7f,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff};//9223372036854775807
|
||||
byte[] b16 = new byte[]{(byte) 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00};//9223372036854775808
|
||||
byte[] b17 = new byte[]{(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff};//18446744073709551615
|
||||
|
||||
byte[] b = new byte[]{(byte)0,(byte)0,(byte)0,(byte)0,(byte)0,(byte)0,(byte)0,(byte)1};//1
|
||||
ByteHelper byteHelper = new ByteHelper();
|
||||
long a = byteHelper.uint64ToLong(b);
|
||||
assertEquals(a,1);
|
||||
|
||||
// byte[] b_test = new byte[]{0x00,0x00,0x00,0x00,(byte) 0x80,0x00,0x00,0x01};//2147483649
|
||||
// long a_test = byteHelper.uint64ToLong(b_test);
|
||||
@@ -538,7 +542,7 @@ import static org.junit.Assert.*;
|
||||
|
||||
}
|
||||
|
||||
//error
|
||||
|
||||
//todo 大于2^64次方无法表示
|
||||
@Test
|
||||
public void uint64ToByteArray() {
|
||||
|
||||
Reference in New Issue
Block a user