分享

short,int,long与byte数组之间的转换 - New - JavaEye论坛

 techres 2011-03-17

引用 http://www./article/83622.htm

 

 

  1. package com.test;
  2. import java.nio.ByteBuffer;
  3. public class ByteUtil {
  4.     /**
  5.      * @param args
  6.      */
  7.     public static void main(String[] args) {
  8.         test2();
  9.     }
  10.     public static void test2()
  11.     {
  12.         short s = -20;
  13.         byte[] b = new byte[2];
  14.         putReverseBytesShort(b, s, 0);
  15.         ByteBuffer buf = ByteBuffer.allocate(2);
  16.         buf.put(b);
  17.         buf.flip();
  18.         System.out.println(getReverseBytesShort(b, 0));
  19.         System.out.println(Short.reverseBytes(buf.getShort()));
  20.         System.out.println("***************************");
  21.         int i = -40;
  22.         b = new byte[4];
  23.         putReverseBytesInt(b, i, 0);
  24.         buf = ByteBuffer.allocate(4);
  25.         buf.put(b);
  26.         buf.flip();
  27.         System.out.println(getReverseBytesInt(b, 0));
  28.         System.out.println(Integer.reverseBytes(buf.getInt()));
  29.         System.out.println("***************************");
  30.         long l = -50;
  31.         b = new byte[8];
  32.         putReverseBytesLong(b, l, 0);
  33.         buf = ByteBuffer.allocate(8);
  34.         buf.put(b);
  35.         buf.flip();
  36.         System.out.println(getReverseBytesLong(b, 0));
  37.         System.out.println(Long.reverseBytes(buf.getLong()));
  38.         System.out.println("***************************");
  39.     }
  40.     public static void test1()
  41.     {
  42.         short s = -20;
  43.         byte[] b = new byte[2];
  44.         putShort(b, s, 0);
  45.         ByteBuffer buf = ByteBuffer.allocate(2);
  46.         buf.put(b);
  47.         buf.flip();
  48.         System.out.println(getShort(b, 0));
  49.         System.out.println(buf.getShort());
  50.         System.out.println("***************************");
  51.         int i = -40;
  52.         b = new byte[4];
  53.         putInt(b, i, 0);
  54.         buf = ByteBuffer.allocate(4);
  55.         buf.put(b);
  56.         buf.flip();
  57.         System.out.println(getInt(b, 0));
  58.         System.out.println(buf.getInt());
  59.         System.out.println("***************************");
  60.         long l = -50;
  61.         b = new byte[8];
  62.         putLong(b, l, 0);
  63.         buf = ByteBuffer.allocate(8);
  64.         buf.put(b);
  65.         buf.flip();
  66.         System.out.println(getLong(b, 0));
  67.         System.out.println(buf.getLong());
  68.         System.out.println("***************************");
  69.     }
  70.     public static void putShort(byte b[], short s, int index) {
  71.         b[index] = (byte) (s >> 8);
  72.         b[index + 1] = (byte) (s >> 0);
  73.     }
  74.     public static void putReverseBytesShort(byte b[], short s, int index) {
  75.         b[index] = (byte) (s >> 0);
  76.         b[index + 1] = (byte) (s >> 8);
  77.     }
  78.     public static short getShort(byte[] b, int index) {
  79.         return (short) (((b[index] << 8) | b[index + 1] & 0xff));
  80.     }
  81.     public static short getReverseBytesShort(byte[] b, int index) {
  82.         return (short) (((b[index+1] << 8) | b[index] & 0xff));
  83.     }
  84.     // ///////////////////////////////////////////////////////
  85.     public static void putInt(byte[] bb, int x, int index) {
  86.         bb[index + 0] = (byte) (x >> 24);
  87.         bb[index + 1] = (byte) (x >> 16);
  88.         bb[index + 2] = (byte) (x >> 8);
  89.         bb[index + 3] = (byte) (x >> 0);
  90.     }
  91.     public static void putReverseBytesInt(byte[] bb, int x, int index) {
  92.         bb[index + 3] = (byte) (x >> 24);
  93.         bb[index + 2] = (byte) (x >> 16);
  94.         bb[index + 1] = (byte) (x >> 8);
  95.         bb[index + 0] = (byte) (x >> 0);
  96.     }
  97.     public static int getInt(byte[] bb, int index) {
  98.         return (int) ((((bb[index + 0] & 0xff) << 24)
  99.                 | ((bb[index + 1] & 0xff) << 16)
  100.                 | ((bb[index + 2] & 0xff) << 8) | ((bb[index + 3] & 0xff) << 0)));
  101.     }
  102.     public static int getReverseBytesInt(byte[] bb, int index) {
  103.         return (int) ((((bb[index + 3] & 0xff) << 24)
  104.                 | ((bb[index + 2] & 0xff) << 16)
  105.                 | ((bb[index + 1] & 0xff) << 8) | ((bb[index + 0] & 0xff) << 0)));
  106.     }
  107.     // /////////////////////////////////////////////////////////
  108.     public static void putLong(byte[] bb, long x, int index) {
  109.         bb[index + 0] = (byte) (x >> 56);
  110.         bb[index + 1] = (byte) (x >> 48);
  111.         bb[index + 2] = (byte) (x >> 40);
  112.         bb[index + 3] = (byte) (x >> 32);
  113.         bb[index + 4] = (byte) (x >> 24);
  114.         bb[index + 5] = (byte) (x >> 16);
  115.         bb[index + 6] = (byte) (x >> 8);
  116.         bb[index + 7] = (byte) (x >> 0);
  117.     }
  118.     public static void putReverseBytesLong(byte[] bb, long x, int index) {
  119.         bb[index + 7] = (byte) (x >> 56);
  120.         bb[index + 6] = (byte) (x >> 48);
  121.         bb[index + 5] = (byte) (x >> 40);
  122.         bb[index + 4] = (byte) (x >> 32);
  123.         bb[index + 3] = (byte) (x >> 24);
  124.         bb[index + 2] = (byte) (x >> 16);
  125.         bb[index + 1] = (byte) (x >> 8);
  126.         bb[index + 0] = (byte) (x >> 0);
  127.     }
  128.     public static long getLong(byte[] bb, int index) {
  129.         return ((((long) bb[index + 0] & 0xff) << 56)
  130.                 | (((long) bb[index + 1] & 0xff) << 48)
  131.                 | (((long) bb[index + 2] & 0xff) << 40)
  132.                 | (((long) bb[index + 3] & 0xff) << 32)
  133.                 | (((long) bb[index + 4] & 0xff) << 24)
  134.                 | (((long) bb[index + 5] & 0xff) << 16)
  135.                 | (((long) bb[index + 6] & 0xff) << 8) | (((long) bb[index + 7] & 0xff) << 0));
  136.     }
  137.     public static long getReverseBytesLong(byte[] bb, int index) {
  138.         return ((((long) bb[index + 7] & 0xff) << 56)
  139.                 | (((long) bb[index + 6] & 0xff) << 48)
  140.                 | (((long) bb[index + 5] & 0xff) << 40)
  141.                 | (((long) bb[index + 4] & 0xff) << 32)
  142.                 | (((long) bb[index + 3] & 0xff) << 24)
  143.                 | (((long) bb[index + 2] & 0xff) << 16)
  144.                 | (((long) bb[index + 1] & 0xff) << 8) | (((long) bb[index + 0] & 0xff) << 0));
  145.     }

}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约