Merge branch 'master' of gitee.com:willfree/min-dev-java

This commit is contained in:
ghy committed 2021-03-05 20:03:08 +08:00
commit 262b2c5194
6 files changed
+329 -60

No files matched your search

+219 -5
View File
@@ -1,6 +1,7 @@
package util;
package main.java.util;
import encoding.VlInt;
import main.java.encoding.VlInt;
/*
* @Author: Wang Feng
@@ -21,7 +22,7 @@ public class ByteHelper {
public static byte getFirstByte(byte[] buffer, VlInt start){
int startInt=start.getVlIntValue2Int();
byte[] firstbyte=new byte[1];
System.arraycopy(buffer,startInt,firstbyte,0,1);
System.arraycopy(buffer,startInt,firstbyte,0,firstbyte.length);
return firstbyte[0];
}
@@ -46,13 +47,226 @@ public class ByteHelper {
return lenbytes;
}
/**
* 无符号int转byte[]
* @param i_int
* @return
*/
private static byte[] intToByteArray(int i_int) {
byte[] result = new byte[4];
//由高位到低位
for(int i=0;i<4;i++) {
int offset=(result.length-i-1)*8;
result[i] = (byte) ((i_int >>>offset) & 0xff);
}
return result;
}
/**
* 无符号byte[]转int
* @param bytes
* @return
*/
private static int byteArrayToInt(byte[] bytes) {
if(bytes.length!=4){
return Integer.parseInt(null);
}
int value= 0;
//由高位到低位
for (int i = 0; i < 4; i++) {
int offset= (bytes.length - 1 - i) * 8;
value +=(bytes[i] & 0x000000ff) << offset; //往高位游
}
return value;
}
/**
* 无符号long转byte[]
* @param l_long
* @return
*/
private static byte[] longToByteArray(long l_long) {
byte[] result = new byte[8];
for (int i = 0; i<8; i++) {
int offset = (result.length - 1 - i) * 8;
result[i] = (byte) ((l_long >>> offset) & 0xff);
}
return result;
}
/**
* 无符号byte[]转long
* @param bytes
* @return
*/
private static long byteArrayToLong(byte[] bytes) {
if(bytes.length!=8){
return Integer.parseInt(null);
}
int value= 0;
//由高位到低位
for (int i = 0; i < 8; i++) {
int offset= (bytes.length - 1 - i) * 8;
value +=(bytes[i] & 0x000000000000ff) << offset; //往高位游
}
return value;
}
/**
* 无符号字节转int
* @param b
* @return
*/
public static int unsignedbyte2int(byte b){
return (int) b;
// private static int unsignedbyte2int(byte b){
// return (int) b&0xff;
// }
/**
* uint8转int
* @param uint8
* @return
*/
public static int uint8ToInt(byte uint8){
return uint8 & 0xff;
}
/**
* 一个uint8转为的int,通过本函数再次转为byte
* @param uint8
* @return
*/
public static byte uint8ToByte(int uint8){
return (byte)(uint8 & 0xff);
}
/**
* uint8转long
* @param uint8
* @return
*/
public static long uint8ToLong(byte uint8){
return uint8ToInt(uint8);
}
/**
* uint16转int
* @param uint16
* @return
*/
public static int uint16ToInt(byte[] uint16){
if(uint16.length!=2){
return Integer.parseInt(null);
}
byte[] bytes=new byte[4];
for(int i=0;i<2;i++) {
bytes[i] = 0x00;
}
System.arraycopy(uint16,0,bytes,2,uint16.length);
return byteArrayToInt(bytes);
}
/**
* 一个uint16转为的int,通过本函数再次转为byte[]
* @param uint16
* @return
*/
public static byte[] uint16ToByteArray(int uint16){
byte[] result = new byte[2];
for (int i = 0; i<2; i++) {
int offset = (result.length - 1 - i) * 8;
result[i] = (byte) ((uint16 >>> offset) & 0xff);
}
return result;
}
/**
* uint16转long
* @param uint16
* @return
*/
public static long uint16ToLong(byte[] uint16){
return uint16ToInt(uint16);
}
/**
* uint32转long
* @param uint32
* @return
*/
public static long uint32ToLong(byte[] uint32){
if(uint32.length!=4){
return Long.parseLong(null);
}
byte[] bytes=new byte[64];
for(int i=0;i<4;i++) {
bytes[i] = 0x00;
}
System.arraycopy(uint32,0,bytes,4,uint32.length);
return byteArrayToLong(bytes);
}
/**
* 一个uint32转为的long,通过本函数再次转为byte[]
* @param uint32
* @return
*/
public static byte[] uint32ToByteArray(long uint32){
byte[] result = new byte[4];
for (int i = 0; i<4; i++) {
int offset = (result.length - 1 - i) * 8;
result[i] = (byte) ((uint32 >>> offset) & 0xff);
}
return result;
}
/**
* uint64转long
* todo: 理论上可能发生越界
* @param uint64
* @return
*/
public static long uint64ToLong(byte[] uint64){
if(uint64.length!=8){
return Long.parseLong(null);
}
return byteArrayToLong(uint64);
}
/**
* 一个uint64转为的long,通过本函数再次转为byte[]
* todo: 理论上可能发生越界
* @param uint64
* @return
*/
public static byte[] uint64ToByteArray(long uint64){
byte[] result = new byte[8];
for (int i = 0; i<8; i++) {
int offset = (result.length - 1 - i) * 8;
result[i] = (byte) ((uint64 >>> offset) & 0xff);
}
return result;
}
// short整型转为byte类型的数组
// private static byte[] unsigned_short_2byte(int length) {
// byte[] targets = new byte[2];
// for (int i = 0; i < 2; i++) {
// int offset = (targets.length - 1 - i) * 8;
// targets[i] = (byte) ((length >>> offset) & 0xff);
// }
// return targets;
// }
//测试:对于uint8,应当测试1 127 128
public static void main(String[] args){
// byte[] testBytes={0x01,0x02};
// for(int i=0;i<2;i++) {
// System.out.println(testBytes[i]&0xFF);
//// System.out.println(Integer.toHexString(testBytes[i]&0xFF));
// }
byte b=-1;
System.out.println(b&0xff);
}
}