short,int,long与byte数组之间的转换

编程技术  /  houtizong 发布于 3年前   85
package com.test; import java.nio.ByteBuffer; public class ByteUtil {     /**      * @param args      */     public static void main(String[] args) {         test2();     }     public static void test2()     {         short s = -20;         byte[] b = new byte[2];         putReverseBytesShort(b, s, 0);         ByteBuffer buf = ByteBuffer.allocate(2);         buf.put(b);         buf.flip();         System.out.println(getReverseBytesShort(b, 0));         System.out.println(Short.reverseBytes(buf.getShort()));         System.out.println("***************************");         int i = -40;         b = new byte[4];         putReverseBytesInt(b, i, 0);         buf = ByteBuffer.allocate(4);         buf.put(b);         buf.flip();         System.out.println(getReverseBytesInt(b, 0));         System.out.println(Integer.reverseBytes(buf.getInt()));         System.out.println("***************************");         long l = -50;         b = new byte[8];         putReverseBytesLong(b, l, 0);         buf = ByteBuffer.allocate(8);         buf.put(b);         buf.flip();         System.out.println(getReverseBytesLong(b, 0));         System.out.println(Long.reverseBytes(buf.getLong()));         System.out.println("***************************");     }     public static void test1()     {         short s = -20;         byte[] b = new byte[2];         putShort(b, s, 0);         ByteBuffer buf = ByteBuffer.allocate(2);         buf.put(b);         buf.flip();         System.out.println(getShort(b, 0));         System.out.println(buf.getShort());         System.out.println("***************************");         int i = -40;         b = new byte[4];         putInt(b, i, 0);         buf = ByteBuffer.allocate(4);         buf.put(b);         buf.flip();         System.out.println(getInt(b, 0));         System.out.println(buf.getInt());         System.out.println("***************************");         long l = -50;         b = new byte[8];         putLong(b, l, 0);         buf = ByteBuffer.allocate(8);         buf.put(b);         buf.flip();         System.out.println(getLong(b, 0));         System.out.println(buf.getLong());         System.out.println("***************************");     }     public static void putShort(byte b[], short s, int index) {         b[index] = (byte) (s >> 8);         b[index + 1] = (byte) (s >> 0);     }     public static void putReverseBytesShort(byte b[], short s, int index) {         b[index] = (byte) (s >> 0);         b[index + 1] = (byte) (s >> 8);     }     public static short getShort(byte[] b, int index) {         return (short) (((b[index] << 8) | b[index + 1] & 0xff));     }     public static short getReverseBytesShort(byte[] b, int index) {         return (short) (((b[index+1] << 8) | b[index] & 0xff));     }     // ///////////////////////////////////////////////////////     public static void putInt(byte[] bb, int x, int index) {         bb[index + 0] = (byte) (x >> 24);         bb[index + 1] = (byte) (x >> 16);         bb[index + 2] = (byte) (x >> 8);         bb[index + 3] = (byte) (x >> 0);     }     public static void putReverseBytesInt(byte[] bb, int x, int index) {         bb[index + 3] = (byte) (x >> 24);         bb[index + 2] = (byte) (x >> 16);         bb[index + 1] = (byte) (x >> 8);         bb[index + 0] = (byte) (x >> 0);     }     public static int getInt(byte[] bb, int index) {         return (int) ((((bb[index + 0] & 0xff) << 24)                 | ((bb[index + 1] & 0xff) << 16)                 | ((bb[index + 2] & 0xff) << 8) | ((bb[index + 3] & 0xff) << 0)));     }     public static int getReverseBytesInt(byte[] bb, int index) {         return (int) ((((bb[index + 3] & 0xff) << 24)                 | ((bb[index + 2] & 0xff) << 16)                 | ((bb[index + 1] & 0xff) << 8) | ((bb[index + 0] & 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 putReverseBytesLong(byte[] bb, long x, int index) {         bb[index + 7] = (byte) (x >> 56);         bb[index + 6] = (byte) (x >> 48);         bb[index + 5] = (byte) (x >> 40);         bb[index + 4] = (byte) (x >> 32);         bb[index + 3] = (byte) (x >> 24);         bb[index + 2] = (byte) (x >> 16);         bb[index + 1] = (byte) (x >> 8);         bb[index + 0] = (byte) (x >> 0);     }     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 long getReverseBytesLong(byte[] bb, int index) {         return ((((long) bb[index + 7] & 0xff) << 56)                 | (((long) bb[index + 6] & 0xff) << 48)                 | (((long) bb[index + 5] & 0xff) << 40)                 | (((long) bb[index + 4] & 0xff) << 32)                 | (((long) bb[index + 3] & 0xff) << 24)                 | (((long) bb[index + 2] & 0xff) << 16)                 | (((long) bb[index + 1] & 0xff) << 8) | (((long) bb[index + 0] & 0xff) << 0));     } } 

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客