物联网开发过程中,会遇到设备上发的数据为16进制、byte类型的数组等类型的数据,实际使用时,需要将这些数据转换为十进制的int类型,在进行其他的业务处理。
但是,转换byte类型的数组时,存在大小端的区别,为了方便使用,写了工具类,特此记录一下。
工具类源代码如下
package com.yann.common.utils;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collections;
import org.apache.commons.lang.ArrayUtils;
/**
*
* @Description: 字节数组转换工具类
* @author yann
* @date 2023年3月27日
*/
public class BytesUtils {
/**
* 转换大端序byte数组
* @param start 数组开始下标
* @param end 数组结束下标
* @param byteParams 数组
* @return
*/
public static long toIntBig(int start,int end,byte[] byteParams){
byte[] bs = new byte[end-start+1];
System.arraycopy(byteParams,start,bs,0,bs.length);
int bytes = bs.length;
switch(bytes) {
case 0:
return 0;
case 1:
return (long)((bs[0] & 0xff));
case 2:
return (long)((bs[0] & 0xff) <<8 | (bs[1] & 0xff));
case 3:
return (long)((bs[0] & 0xff) <<16 | (bs[1] & 0xff) <<8 | (bs[2] & 0xff));
case 4:
return (long)((bs[0] & 0xffL) <<24 | (bs[1] & 0xffL) << 16 | (bs[2] & 0xffL) <<8 | (bs[3] & 0xffL));
case 5:
return (long)((bs[0] & 0xffL) <<32 | (bs[1] & 0xffL) <<24 | (bs[2] & 0xffL) << 16 | (bs[3] & 0xffL) <<8 | (bs[4] & 0xffL));
case 6:
return (long)((bs[0] & 0xffL) <<40 | (bs[1] & 0xffL) <<32 | (bs[2] & 0xffL) <<24 | (bs[3] & 0xffL) << 16 | (bs[4] & 0xffL) <<8 | (bs[5] & 0xffL));
case 7:
return (long)((bs[0] & 0xffL) <<48 | (bs[1] & 0xffL) <<40 | (bs[2] & 0xffL) <<32 | (bs[3] & 0xffL) <<24 | (bs[4] & 0xffL) << 16 | (bs[5] & 0xffL) <<8 | (bs[6] & 0xffL));
case 8:
return (long)((bs[0] & 0xffL) <<56 | (bs[1] & 0xffL) << 48 | (bs[2] & 0xffL) <<40 | (bs[3] & 0xffL)<<32 |
(bs[4] & 0xffL) <<24 | (bs[5] & 0xffL) << 16 | (bs[6] & 0xffL) <<8 | (bs[7] & 0xffL));
default:
return 0;
}
}
/**
* 转换小端序byte数组
* @param start 数组开始下标
* @param end 数组结束下标
* @param byteParams 数组
* @return
*/
public static long toIntSmall(int start,int end,byte[] byteParams){
byte[] bs = new byte[end-start+1];
System.arraycopy(byteParams,start,bs,0,bs.length);
int bytes = bs.length;
switch(bytes) {
case 0:
return 0;
case 1:
return (long)((bs[0] & 0xff));
case 2:
return (long)((bs[0] & 0xff) | (bs[1] & 0xff)<<8);
case 3:
return (long)((bs[0] & 0xff) | (bs[1] & 0xff) <<8 | (bs[2] & 0xff) <<16);
case 4:
return (long)((bs[0] & 0xffL) | (bs[1] & 0xffL) <<8 | (bs[2] & 0xffL) << 16 | (bs[3] & 0xffL) <<24 );
case 5:
return (long)((bs[0] & 0xffL) | (bs[1] & 0xffL) <<8 | (bs[2] & 0xffL) << 16 | (bs[3] & 0xffL) <<24 | (bs[4] & 0xffL) << 32);
case 6:
return (long)((bs[0] & 0xffL) | (bs[1] & 0xffL) <<8 | (bs[2] & 0xffL) <<16 | (bs[3] & 0xffL) << 24 | (bs[4] & 0xffL) <<32 | (bs[5] & 0xffL) << 40);
case 7:
return (long)((bs[0] & 0xffL) | (bs[1] & 0xffL) <<8 | (bs[2] & 0xffL) <<16 | (bs[3] & 0xffL) <<24 | (bs[4] & 0xffL) << 32 | (bs[5] & 0xffL) <<40 | (bs[6] & 0xffL) << 48);
case 8:
return (long)((bs[0] & 0xffL) | (bs[1] & 0xffL) << 8 | (bs[2] & 0xffL) <<16 | (bs[3] & 0xffL)<<24 |
(bs[4] & 0xffL) <<32 | (bs[5] & 0xffL) << 40 | (bs[6] & 0xffL) <<48 | (bs[7] & 0xffL) << 56);
default:
return 0;
}
}
public static long toIntSmall(byte[] byteParams){
return toIntSmall(0,byteParams.length-1,byteParams);
}
public static long toIntBig(byte[] byteParams){
return toIntBig(0,byteParams.length-1,byteParams);
}
public static void main(String[] aa){
test1();
}
public static void test1(){
byte[] a ={(byte) 0x11};
System.out.println("1a:");
System.out.println("small:"+toIntSmall(0,a.length-1,a));
System.out.println("big:"+toIntBig(0,a.length-1,a));
byte[] b = {(byte) 0x11,(byte) 0x12};
System.out.println("2b:");
System.out.println("small:"+toIntSmall(b));
System.out.println("big:"+toIntBig(b));
byte[] c = {(byte) 0x11,(byte) 0x12,(byte) 0x13};
System.out.println("3c:");
System.out.println("small:"+toIntSmall(0,c.length-1,c));
System.out.println("big:"+toIntBig(0,c.length-1,c));
byte[] d = {(byte) 0x11,(byte) 0x12,(byte) 0x13,(byte) 0x14};
System.out.println("4d:");
System.out.println("small:"+toIntSmall(0,d.length-1,d));
System.out.println("big:"+toIntBig(0,d.length-1,d));
byte[] e = {(byte) 0x11,(byte) 0x12,(byte) 0x13,(byte) 0x14,(byte) 0x15};
System.out.println("5e:");
System.out.println("small:"+toIntSmall(0,e.length-1,e));
System.out.println("big:"+toIntBig(0,e.length-1,e));
byte[] f = {(byte) 0x11,(byte) 0x12,(byte) 0x13,(byte) 0x14,(byte) 0x15,(byte) 0x16};
System.out.println("6f:");
System.out.println("small:"+toIntSmall(0,f.length-1,f));
System.out.println("big:"+toIntBig(0,f.length-1,f));
byte[] g = {(byte) 0x11,(byte) 0x12,(byte) 0x13,(byte) 0x14,(byte) 0x15,(byte) 0x16,(byte) 0x17};
System.out.println("7g:");
System.out.println("small:"+toIntSmall(0,g.length-1,g));
System.out.println("big:"+toIntBig(0,g.length-1,g));
byte[] h = {(byte) 0x11,(byte) 0x12,(byte) 0x13,(byte) 0x14,(byte) 0x15,(byte) 0x16,(byte) 0x17,(byte) 0x18};
System.out.println("8h:");
System.out.println("small:"+toIntSmall(0,h.length-1,h));
System.out.println("big:"+toIntBig(0,h.length-1,h));
}
}