mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
Merge branch 'master' of gitee.com:willfree/min-dev-java
This commit is contained in:
commit
84ae7d1443
18 files changed
+750
-1
No files matched your search
@@ -0,0 +1,271 @@
|
||||
package util;
|
||||
|
||||
import encoding.VlInt;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:常用的数据类型转换。转无符号byte是重点
|
||||
* @Version: 1.0.0
|
||||
* @Date: 15:40 2021/3/2
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class ByteHelper {
|
||||
|
||||
/**
|
||||
* 从byte[]数组指定开始位置获取第一个字节
|
||||
* todo: start位置可能是大于int类型的数值
|
||||
* @param buffer
|
||||
* @param start
|
||||
* @return
|
||||
*/
|
||||
public static byte getFirstByte(byte[] buffer, VlInt start){
|
||||
int startInt=start.getVlIntValue2Int();
|
||||
byte[] firstbyte=new byte[1];
|
||||
System.arraycopy(buffer,startInt,firstbyte,0,firstbyte.length);
|
||||
return firstbyte[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* 从byte[]数组指定开始位置获取长度为len的字节数组
|
||||
* todo: start位置可能是大于int类型的数值
|
||||
* @param buffer
|
||||
* @param start
|
||||
* @return
|
||||
*/
|
||||
public static byte[] getLenBytes(byte[] buffer, VlInt start,VlInt len){
|
||||
int startInt=start.getVlIntValue2Int();
|
||||
int lenInt=len.getVlIntValue2Int();
|
||||
byte[] lenbytes=new byte[1];
|
||||
System.arraycopy(buffer,startInt,lenbytes,0,lenInt);
|
||||
return lenbytes;
|
||||
}
|
||||
public static byte[] getLenBytes(byte[] buffer, VlInt start,int len){
|
||||
int startInt=start.getVlIntValue2Int();
|
||||
byte[] lenbytes=new byte[1];
|
||||
System.arraycopy(buffer,startInt,lenbytes,0,len);
|
||||
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
|
||||
*/
|
||||
// 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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user