ByteTool.java


public class ByteTool {
    /**
    * byte to int
    *
    * @param b
    *            待转换的字节数组
    * @param offset
    *            偏移量,字节数组中开始转换的位置
    * @return
    */
    public static int byte2int(byte b[], int offset) {
       return b[offset + 3] & 0xff | (b[offset + 2] & 0xff) << 8
         | (b[offset + 1] & 0xff) << 16 | (b[offset] & 0xff) << 24;
    }
    public static short byte2short(byte b[], int offset) {
           return (short)(b[offset + 1] & 0xff | (b[offset] & 0xff) << 8);
        }

    /**
    * int to byte
    *
    * @param n待转换的整形变量
    * @param buf
    *            转换后生成的字节数组
    * @param offset
    *            偏移量,字节数组中开始存放的位置
    */
    public static void int2byte(int n, byte buf[], int offset) {
       buf[offset] = (byte) (n >> 24);
       buf[offset + 1] = (byte) (n >> 16);
       buf[offset + 2] = (byte) (n >> 8);
       buf[offset + 3] = (byte) n;
    }

    /**
    * @returntype void
    * @param n
    *            待转换的short变量
    * @param buf
    *            转换后存放的byte数组
    * @param offset偏移量,字节数组中开始存放的位置
    */
    public static void short2byte(int n, byte buf[], int offset) {
       buf[offset] = (byte) (n >> 8);
       buf[offset + 1] = (byte) n;
    }

    /**
    *
    * @param buf
    * @return
    */
    public static String byte2Hex(byte[] buf) {
       StringBuffer sb = new StringBuffer();
       //sb.append("{");
       for (byte b : buf) {
        if (b == 0) {
         sb.append("00");
        } else if (b == -1) {
         sb.append("FF");
        } else {
         String str = Integer.toHexString(b).toUpperCase();
         // sb.append(a);
         if (str.length() == 8) {
          str = str.substring(6, 8);
         } else if (str.length() < 2) {
          str = "0" + str;
         }
         sb.append(str);

        }
        //sb.append(" ");
       }
       //sb.append("}");
       return sb.toString();
    }

    public static int unsignedByteToInt(byte b) {
       return (int) b & 0xFF;
    }

    /**
    * convert signed one byte into a hexadecimal digit
    *
    * @param b
    *            byte
    * @return convert result
    */
    public static String byteToHex(byte b) {
       int i = b & 0xFF;
       return Integer.toHexString(i);
    }

    /**
    * convert signed 4 bytes into a 32-bit integer
    *
    * @param buf
    *            bytes buffer
    * @param pos
    *            beginning <code>byte</code>> for converting
    * @return convert result
    */
    public static long unsigned4BytesToInt(byte[] buf, int pos) {
       int firstByte = 0;
       int secondByte = 0;
       int thirdByte = 0;
       int fourthByte = 0;
       int index = pos;
       firstByte = (0x000000FF & ((int) buf[index]));
       secondByte = (0x000000FF & ((int) buf[index + 1]));
       thirdByte = (0x000000FF & ((int) buf[index + 2]));
       fourthByte = (0x000000FF & ((int) buf[index + 3]));
       index = index + 4;
       return ((long) (firstByte << 24 | secondByte << 16 | thirdByte << 8 | fourthByte)) & 0xFFFFFFFFL;
    }

    public static long bytes2long(byte[] b) {

       int mask = 0xff;
       long temp = 0;
       long res = 0;
       for (int i = 0; i < 8; i++) {
        res <<= 8;
        temp = b[i] & mask;
        res |= temp;
       }
       return res;
    }

    public static byte[] long2bytes(long num) {
       byte[] b = new byte[8];
       for (int i = 0; i < 8; i++) {
        b[i] = (byte) (num >>> (56 - i * 8));
       }
       return b;
    }

    public static long getLong(byte[] bb, int index) {
       return ((((long) bb[index + 0] & 0xff) << 56)
         | (((long) bb[index + 1] & 0xff) << 48)
         | (((long) bb[index + 2] & 0xff) << 40)
         | (((long) bb[index + 3] & 0xff) << 32)
         | (((long) bb[index + 4] & 0xff) << 24)
         | (((long) bb[index + 5] & 0xff) << 16)
         | (((long) bb[index + 6] & 0xff) << 8) | (((long) bb[index + 7] & 0xff) << 0));
    }

    public static void putLong(byte[] bb, long x, int index) {
       bb[index + 0] = (byte) (x >> 56);
       bb[index + 1] = (byte) (x >> 48);
       bb[index + 2] = (byte) (x >> 40);
       bb[index + 3] = (byte) (x >> 32);
       bb[index + 4] = (byte) (x >> 24);
       bb[index + 5] = (byte) (x >> 16);
       bb[index + 6] = (byte) (x >> 8);
       bb[index + 7] = (byte) (x >> 0);
    }

    public static void putShort(byte b[], short s, int index) {
       b[index] = (byte) (s >> 8);
       b[index + 1] = (byte) (s >> 0);
    }

    public static short getShort(byte[] b, int index) {
       return (short) (((b[index] << 8) | b[index + 1] & 0xff));
    }
    
    public static byte[] subArray(byte[] srcByte,int nStart, int nLength ){
        
        byte[] ret = new byte[nLength];
        System.arraycopy(srcByte, nStart, ret, 0, nLength);
        return ret;
    }
    
    public static void main(String[] args){
        
        byte[] b=new byte[3];
        int year_offset=15;   //2*2*2*2=16     > 16year[support:2011~2026] 00001011
        int month=12;         //2*2*2*2=16     > 12months                  00000111
        int day=31;           //2*2*2*2*2=32   > 31days                    00011111
        int hour=24;          //2*2*2*2*2=32   > 24hours                   00010001
        int min=59;            //2*2*2*2*2*2=64 > 60minitues                00001001
        
//        int year_offset=1;   //2*2*2*2=16     > 16year[support:2011~2026] 00001011
//        int month=1;         //2*2*2*2=16     > 12months                  00000111
//        int day=1;           //2*2*2*2*2=32   > 31days                    00011111
//        int hour=0;          //2*2*2*2*2=32   > 24hours                   00010001
//        int min=0;            //2*2*2*2*2*2=64 > 60minitues                00001001
        
        //int temp=(int)(day<<11)+(int)(hour<<6)+(int)(min<<0);
        int temp=(day<<11)+(hour<<6)+(min<<0);
        System.out.println( "temp="+temp );
        b[0] = (byte)((year_offset<<4)|month);
        b[1] = (byte)(temp >> 8);
        b[2] = (byte)(temp >> 0);
        
        int temp2=(int) (
                    ((((int)b[1])&0xFF) << 8) |
                    (((int)b[2])&0xFF));
        
        System.out.println( "temp2="+temp2 );
        int year_offset_reverse=(byte)((b[0] >> 4)& 0x0F);
        int month_reverse=(byte)(b[0] & 0x0F);
        int day_reverse=(byte)(temp2>>11);
        //0000 0 111 11 00 0000  0x
        int hour_reverse=(byte)((byte)((temp2 & 0x07C0) >>6));
        //00000 00000 11111
        //
        int min_reverse=(byte)(((temp2 & 0x003F)));
        
        System.out.println( "year_offset_reverse="+year_offset_reverse );
        System.out.println( "month_reverse="+month_reverse );
        System.out.println( "day_reverse="+day_reverse );
        System.out.println( "hour_reverse="+hour_reverse );
        System.out.println( "min_reverse="+min_reverse );
        
        /*
        b[0] = (byte)((year_offset<<4)|month);
        //day-hour-min :2bytes :DAY[4BIT]+HOUR[low-4-BIT] - HOUR[hign-2-BIT]+MIN[6BIT]
        
        b[1] = (byte)((day << 3)|hour << 4);
        b[2] = (byte)(((min << 2))|hour>> 4);
        
        
        char year_offset_reverse=(char)(b[0] >> 4);
        char month_reverse=(char)(b[0] & 0x0F);
        char day_reverse=(char)(b[1] >> 3);
        char hour_reverse=(char)((b[2] << 6) | (b[1] & 0x0F));
        char min_reverse=(char)(b[1] >> 2);
        
        System.out.println( "year_offset_reverse="+year_offset_reverse );
        System.out.println( "month_reverse="+month_reverse );
        System.out.println( "day_reverse="+day_reverse );
        System.out.println( "hour_reverse="+hour_reverse );
        System.out.println( "min_reverse="+min_reverse );
        */
        
        
        /*
        b[0] = (byte)((year_offset<<4)|month);
        //day-hour-min :2bytes :DAY[4BIT]+HOUR[low-4-BIT] - HOUR[hign-2-BIT]+MIN[6BIT]
        
        b[1] = (byte)((day << 3)|hour << 4);
        b[2] = (byte)(((min << 2))|hour>> 4);
        
        
        char year_offset_reverse=(char)(b[0] >> 4);
        char month_reverse=(char)(b[0] & 0x0F);
        char day_reverse=(char)(b[1] >> 3);
        char hour_reverse=(char)((b[2] << 6) | (b[1] & 0x0F));
        char min_reverse=(char)(b[1] >> 2);
        
        System.out.println( "year_offset_reverse="+year_offset_reverse );
        System.out.println( "month_reverse="+month_reverse );
        System.out.println( "day_reverse="+day_reverse );
        System.out.println( "hour_reverse="+hour_reverse );
        System.out.println( "min_reverse="+min_reverse );
        
        */
        
        
        
        
//        short s=260;
//        
//        b[0] = (byte) (s >> 8);
//        b[1] = (byte) (s >> 0);
//        short s2=(short) (b[0] << 8);
//        short SS=(short) (((b[0] << 8) | b[1] & 0xff));
//        
//        System.out.println(Long.MAX_VALUE);
//        byte[] bs = long2bytes(Long.MAX_VALUE);
//        System.out.println( bytes2long(bs) );
    }
}

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 、 1资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READmE.文件(md如有),本项目仅用作交流学习参考,请切勿用于商业用途。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FocusOneThread

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值