From 0e93a55428ff8d45b8946e57a511828902fcd9f7 Mon Sep 17 00:00:00 2001 From: zhaofeng233 Date: Mon, 15 Mar 2021 01:27:30 +0800 Subject: [PATCH] =?UTF-8?q?ByteHelper=E6=96=B0=E5=8F=91=E7=8E=B0=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/util/ByteHelper.java | 31 +++++++++++++++++--------- src/test/java/encoding/BlockTest.java | 12 +++++----- src/test/java/util/ByteHelperTest.java | 16 ++++++++----- 3 files changed, 37 insertions(+), 22 deletions(-) diff --git a/src/main/java/util/ByteHelper.java b/src/main/java/util/ByteHelper.java index 134bb48..22eb1eb 100644 --- a/src/main/java/util/ByteHelper.java +++ b/src/main/java/util/ByteHelper.java @@ -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; + } } diff --git a/src/test/java/encoding/BlockTest.java b/src/test/java/encoding/BlockTest.java index 4ebdff8..b132d18 100644 --- a/src/test/java/encoding/BlockTest.java +++ b/src/test/java/encoding/BlockTest.java @@ -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); } diff --git a/src/test/java/util/ByteHelperTest.java b/src/test/java/util/ByteHelperTest.java index 689b53e..0b8c1d5 100644 --- a/src/test/java/util/ByteHelperTest.java +++ b/src/test/java/util/ByteHelperTest.java @@ -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() {