package com.dy.common.util; public class ByteUtilUnsigned { /** * 大端模式《数据低位在数组高字节》 * 无符号int类型转换成4位byte数组 * java没有无符号整型数据,只有有符号整数,取值范围是-2147483648~2147483647 * C有无符号整型数据,取值范围是0到4294967295,已经超出了java的有符号整数上限,所以只能用java的long型表示无符号整数 * @value bs byte[] * @value value int int类型的参数 * @value from int * @throws Exception 异常 */ public static void int2Bytes_BE(byte[] bs, long value, int from)throws Exception { int len = 4 ; Long maxIntUnsigned = Long.valueOf(Integer.MAX_VALUE * 2 + 1) ; Long minIntUnsigned = Long.valueOf(Integer.MIN_VALUE * 2) ; if(value < minIntUnsigned || value > maxIntUnsigned ){ throw new Exception("数据" + value + "超出了无符号Int型的取值范围(" + minIntUnsigned + "~" + maxIntUnsigned + ")") ; } int temp ; if(value > Integer.MAX_VALUE){ temp = (Long.valueOf(value - (Integer.MAX_VALUE * 2 + 1) - 1)).intValue() ; }else{ temp = Long.valueOf(value).intValue() ; } boolean b = isOutOfArrLength(bs.length, (from - 1) + len); if (!b) { for (int i = (len - 1); i >= 0; i--) { bs[from + i] = Integer.valueOf(temp & 0xff).byteValue();//将最低位保存在高字节 temp = temp >> 8; // 向右移8位 } } else { throw new Exception("int2Bytes时数组越界"); } } /** * 小端模式《数据低位在数组低字节》 * 与方法int2Bytes算法一样,只是把顺序反过来 * @value bs byte[] * @value value int int类型的参数 * @value from int */ public static void int2Bytes_LE(byte[] bs, long value, int from)throws Exception { int len = 4 ; Long maxIntUnsigned = Long.valueOf(Integer.MAX_VALUE) * 2 + 1; Long minIntUnsigned = Long.valueOf(Integer.MIN_VALUE) * 2 ; if(value < minIntUnsigned || value > maxIntUnsigned ){ throw new Exception("数据" + value + "超出了无符号Int型的取值范围(" + minIntUnsigned + "~" + maxIntUnsigned + ")") ; } int temp ; if(value > Integer.MAX_VALUE){ temp = (Long.valueOf(value - (Integer.MAX_VALUE * 2 + 1) - 1)).intValue() ; }else{ temp = Long.valueOf(value).intValue() ; } boolean b = isOutOfArrLength(bs.length, (from - 1) + len); if (!b) { for (int i = 0; i <= len ; i++) { bs[from + i] = Integer.valueOf(temp & 0xff).byteValue();// 将最低位保存在低字节 temp = temp >> 8; // 向右移8位 } } else { throw new Exception("int2Bytes时数组越界"); } } /** * 大端模式《数据低位在数组高字节》 * 4位字节数组转换为整型 * @param bs 字节数组 * @param from 起始位置 * @return 结果 */ public static long bytes2Int_BE(byte[] bs, int from) throws Exception { boolean b = isOutOfArrLength(bs.length, (from - 1) + 4); if (!b) { long s = 0; long s0 = bs[from + 0] & 0xFF ;// 数据的最高位在低字节 long s1 = bs[from + 1] & 0xFF ; long s2 = bs[from + 2] & 0xFF ; long s3 = bs[from + 3] & 0xFF ; // 最低位S3不变 s0 <<= 24; s1 <<= 16; s2 <<= 8; s = s0 | s1 | s2 | s3; if(s < 0){ //s = Integer.MAX_VALUE -s ; s = Integer.MAX_VALUE * 2 + 1 + s + 1 ; } return s; } else { throw new Exception("byte2Int时数组越界"); } } /** * 小端模式《数据低位在数组低字节》 * 与方法bytes2Int算法一样,只是把顺序反过来 * @param bs 字节数组 * @param from 字节数组起始位置 * @return */ public static long bytes2Int_LE(byte[] bs, int from) throws Exception { boolean b = isOutOfArrLength(bs.length, (from - 1) + 4); if (!b) { long s = 0; long s0 = bs[from + 0] & 0xFF ;// 数据的最低位在低字节 long s1 = bs[from + 1] & 0xFF ; long s2 = bs[from + 2] & 0xFF ; long s3 = bs[from + 3] & 0xFF ; // S0不变 s1 <<= 8; s2 <<= 16; s3 <<= 24; s = s0 | s1 | s2 | s3; if(s < 0){ //s = Integer.MAX_VALUE -s ; s = Integer.MAX_VALUE * 2 + 1 + s + 1 ; } return s; } else { throw new Exception("byte2Int时数组越界"); } } /** * 小端模式《数据低位在数组低字节》 * 与方法bytes2Int算法一样,只是把顺序反过来 * @param bs 字节数组 * @param from 字节数组起始位置 * @param has3Byte 是否包含数据下标为3的字节,如果不包含实际处理的是3字节的整数 * @return */ public static long bytes2Int_LE(byte[] bs, int from, boolean has3Byte) throws Exception { boolean b = isOutOfArrLength(bs.length, (from - 1) + 4); if (!b) { long s = 0; long s0 = bs[from + 0] & 0xFF ;// 数据的最低位在低字节 long s1 = bs[from + 1] & 0xFF ; long s2 = bs[from + 2] & 0xFF ; long s3 = 0L ; if(!has3Byte){ s3 = bs[from + 3] & 0xFF ; } // S0不变 s1 <<= 8; s2 <<= 16; s3 <<= 24; s = s0 | s1 | s2 | s3; if(s < 0){ //s = Integer.MAX_VALUE -s ; s = Integer.MAX_VALUE * 2 + 1 + s + 1 ; } return s; } else { throw new Exception("byte2Int时数组越界"); } } /** * 小端模式《数据低位在数组低字节》 * 与方法bytes2Int算法一样,只是把顺序反过来 * @param bs * @return */ public static long bytes2Long_LE(byte[] bs, int startIndex, int endIndex) throws Exception { boolean b = isOutOfArrLength(bs.length, endIndex); if (!b) { byte count = 0 ; long s = 0; long temp = 0 ; for(int i = startIndex ; i <= endIndex; i++){ temp = bs[i] & 0xFF ;// 数据的最低位在低字节 if(temp < 0){ temp = Byte.MAX_VALUE * 2 + 1 + temp + 1 ; } if(count > 0){ temp <<= count * 8; } count++ ; s = s | temp ; } return s; } else { throw new Exception("bytes2Long_LE时数组越界"); } } /** * 大端模式《数据低位在数组高字节》 * 无符号short类型转换成2位byte数组 * java没有无符号短整型数据,只有有符号短整数,取值范围是-32768~32767 * 若模拟无符号短整型数据,取值范围是0到65535 ,已经超出了java的有符号整数上限,所以只能用java的Int型表示无符号整数 * @value bs byte[] */ public static long bytes2Long_BE(byte[] bs, int startIndex, int endIndex) throws Exception { boolean b = isOutOfArrLength(bs.length, endIndex); if (!b) { int count = endIndex - startIndex ; long s = 0; long temp = 0 ; for(int i = startIndex ; i <= endIndex ; i++){ temp = bs[i] & 0xFF ;// 数据的最低位在低字节 if(temp < 0){ temp = Byte.MAX_VALUE * 2 + 1 + temp + 1 ; } if(count > 0){ temp <<= count * 8; } count-- ; s = s | temp ; } return s; } else { throw new Exception("bytes2Long_LE时数组越界"); } } // public static long bytes2Int_BE(byte[] bs, int from) throws Exception { // boolean b = isOutOfArrLength(bs.length, (from - 1) + 4); // if (!b) { // long s = 0; // long s0 = bs[from + 0] & 0xFF ;// 数据的最高位在低字节 // long s1 = bs[from + 1] & 0xFF ; // long s2 = bs[from + 2] & 0xFF ; // long s3 = bs[from + 3] & 0xFF ; // // // 最低位S3不变 // s0 <<= 24; // s1 <<= 16; // s2 <<= 8; // s = s0 | s1 | s2 | s3; // if(s < 0){ // //s = Integer.MAX_VALUE -s ; // s = Integer.MAX_VALUE * 2 + 1 + s + 1 ; // } // return s; // } else { // throw new Exception("byte2Int时数组越界"); // } // } /** * 大端模式《数据低位在数组高字节》 * 无符号short类型转换成2位byte数组 * java没有无符号短整型数据,只有有符号短整数,取值范围是-32768~32767 * 若模拟无符号短整型数据,取值范围是0到65535 ,已经超出了java的有符号整数上限,所以只能用java的Int型表示无符号整数 * @value bs byte[] * @value value int int类型的参数 * @value from int */ public static void short2Bytes_BE(byte[] bs, int value, int from)throws Exception { int maxShortUnsigned = Integer.valueOf(Short.MAX_VALUE) * 2 + 1; int minShortUnsigned = Integer.valueOf(Short.MIN_VALUE) * 2 ; if(value < minShortUnsigned || value > maxShortUnsigned ){ throw new Exception("数据" + value + "超出了无符号short型的取值范围(" + minShortUnsigned + "~" + maxShortUnsigned + ")") ; } short temp = 0 ; if(value > Short.MAX_VALUE){ temp = (Integer.valueOf(value - (Short.MAX_VALUE * 2 + 1) - 1)).shortValue() ;//(Integer.valueOf(Short.MAX_VALUE - value)).shortValue() ; }else{ temp = Integer.valueOf(value).shortValue() ; } boolean b = isOutOfArrLength(bs.length, (from - 1) + 2); if (!b) { for (int i = 1; i >= 0; i--) { bs[from + i] = Integer.valueOf(temp & 0xff).byteValue();//将最低位保存在高字节 temp = (short)(temp >> 8); // 向右移8位 } } else { throw new Exception("short2Bytes时数组越界"); } } /** * 小端模式《数据低位在数组低字节》 * 与方法short2Bytes算法一样,只是把顺序反过来 * @value bs byte[] * @value value int int类型的参数 * @value from int */ public static void short2Bytes_LE(byte[] bs, int value, int from)throws Exception { int len = 2 ; int maxShortUnsigned = Integer.valueOf(Short.MAX_VALUE) * 2 + 1; int minShortUnsigned = Integer.valueOf(Short.MIN_VALUE) * 2 ; if(value < minShortUnsigned || value > maxShortUnsigned ){ throw new Exception("数据" + value + "超出了无符号short型的取值范围(" + minShortUnsigned + "~" + maxShortUnsigned + ")") ; } short temp = 0 ; if(value > Short.MAX_VALUE){ temp = (Integer.valueOf(value - (Short.MAX_VALUE * 2 + 1) - 1)).shortValue() ;//(Integer.valueOf(Short.MAX_VALUE - value)).shortValue() ; }else{ temp = Integer.valueOf(value).shortValue() ; } boolean b = isOutOfArrLength(bs.length, (from - 1) + len); if (!b) { for (int i = 0; i < len; i++) { bs[from + i] = Integer.valueOf(temp & 0xff).byteValue();//将数据低位保存在数据低字节 temp = (short)(temp >> 8); // 向右移8位 } } else { throw new Exception("short2Bytes时数组越界"); } } /** * 大端模式《数据低位在数组高字节》 * 2位字节数组转换为短整型 * @param bs 字节数组 * @param from 字节数组起始位置 * @return 结果 */ public static int bytes2Short_BE(byte[] bs, int from) throws Exception { boolean b = isOutOfArrLength(bs.length, (from - 1) + 2); if (!b) { int s = 0; int s0 = Integer.valueOf(bs[from + 0] & 0xff).shortValue();// 最低位 int s1 = Integer.valueOf(bs[from + 1] & 0xff).shortValue(); // 最低位S1不变 s0 <<= 8; s = s0 | s1 ; if(s < 0){ s = (Short.MAX_VALUE * 2 + 1) + s + 1; } return s; } else { throw new Exception("bytes2Short时数组越界"); } } /** * 小端模式《数据低位在数组低字节》 * 与方法bytes2Short算法一样,只是把顺序反过来 * @param bs 字节数组 * @param from 字节数组起始位置 * @return 结果 */ public static int bytes2Short_LE(byte[] bs, int from) throws Exception { boolean b = isOutOfArrLength(bs.length, (from - 1) + 2); if (!b) { int s = 0; int s0 = Integer.valueOf(bs[from + 0] & 0xff).shortValue();// 小下标字节转数据低位 int s1 = Integer.valueOf(bs[from + 1] & 0xff).shortValue();// 大下标字节转数据高位 // 最低位S0不变 s1 <<= 8; s = s1 | s0 ; if(s < 0){ s = (Short.MAX_VALUE * 2 + 1) + s + 1; } return s; } else { throw new Exception("bytes2Short时数组越界"); } } /** * 1位字节数组转换为短整型 * @param bs 字节数组 * @param index 字节数组起始位置 * @return 结果 */ public static short byte2Byte(byte[] bs, int index) throws Exception { if (bs.length - 1 < index) { throw new Exception("byte2Short(byte[] bs, int index)时数组越界"); } else { byte bv = (byte)(bs[index] & 0xff) ; short s = 0 ; if(bv < 0){ s = (short)(Byte.MAX_VALUE * 2 + 1 + bv + 1) ; }else{ s = bv ; } return s ; } } /** * 无符号short类型转换成1位byte * java没有无符号短整型数据,只有有符号短整数,取值范围是-128~127 * 若模拟无符号短整型数据,取值范围是0到255 ,已经超出了java的有符号整数上限,所以只能用java的short型表示无符号整数 * @value bs byte[] 字节数组 * @value value short short类型的参数 * @value index 起始位置 */ public static void byte2Byte(byte[] bs, short value, int index)throws Exception { int maxShortUnsigned = Integer.valueOf(Byte.MAX_VALUE) * 2 + 1; int minShortUnsigned = Integer.valueOf(Byte.MIN_VALUE) * 2 ; if(value < minShortUnsigned || value > maxShortUnsigned ){ throw new Exception("数据" + value + "超出了无符号byte型的取值范围(" + minShortUnsigned + "~" + maxShortUnsigned + ")") ; } if (bs.length - 1 < index) { throw new Exception("byte2Byte(byte[] bs, short value, int index)时数组越界"); } else { bs[index] = Integer.valueOf(value & 0xff).byteValue() ; } } /** * 判断所有字节是否为0xFF * @param bs 字节数组 * @param index 起始位置 * @param len 长度 * @return 结果 * @throws Exception */ public static boolean bytesIsAll0xFF(byte[] bs, int index, int len)throws Exception { int count = 0 ; for(int i = index; i < index + len; i++){ if(bs[i] == (byte)0xFF){ count++ ; } } return count==len?true:false ; } /** * 判断数组下标是否越界 * * @value bsLength 数组总长度 * @value toSite 数组偏移量 * @return 结果 */ private static boolean isOutOfArrLength(int bsLength, int toSite) { if (bsLength > toSite) { return false; } else { return true; } } public static void main(String[] args) throws Exception{ byte[] bs = new byte[]{(byte)0x38,(byte)0x1d,(byte)0x00,(byte)0x00,(byte)0x00} ; Long lg1 = ByteUtilUnsigned.bytes2Long_LE(bs, 0, 4) ; Long lg2 = ByteUtilUnsigned.bytes2Long_BE(bs, 0, 4) ; System.out.println(lg1); System.out.println(lg2); } // public static void main(String[] args) throws Exception{ //// int d = 123456; //// byte[] bs = new byte[4] ; //// int2Bytes_BE(bs, d, 0) ; //// System.out.println(ByteUtil.bytes2Hex(bs, false)); //// long dd = bytes2Int_BE(bs, 0) ; //// System.out.println(dd); //// //// byte[] bb = new byte[1] ; //// bb[0] = (byte)255 ; //// short s = byte2Byte(bb, 0); //// System.out.println(s); // // byte[] bs = new byte[]{(byte)0x00, (byte)0x00, (byte)0x3A, (byte)0x88} ; // //byte[] bs = new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff} ; // boolean flag = ByteUtilUnsigned.bytesIsAll0xFF(bs, 0, 4) ; // System.out.println(flag); // Long s = ByteUtilUnsigned.bytes2Int_BE(bs, 0); // System.out.println(s); // // // } }