JR 精品文章 - short,int,long与byte数组之间的转换
AD: jr (at) javaresearch.org


首页 | 动态 | 文章 | FAQ  | 新闻 | 下载 | 代码 | 工作 | 调查 | 术语 | 站点 | 图书 | 论坛 | 帮助 | 全部  

TOP | 交流 | 软件 | 专栏 | 开源 | 译/著 | 源码 | API  | 推荐 | FTP  | 积分 | 统计 | 搜索 | Blog | 我们  
首页 » 研究文集 » J2SE综合 搜索标题相关文章 搜索标题相关文章    评论此文章 发表评论     开始监控此文章 开始监控   加入收藏夹  加入收藏夹
short,int,long与byte数组之间的转换
xiaoyuer 原创   更新:2008-01-02 17:43:16  版本: 2.0   

  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.     }
  146. }


版权声明   给作者写信
本篇文章对您是否有帮助?  投票:         投票结果:     12       1
作者其它文章: 作者全部文章
评论人:liuxing521a 发表时间: Fri May 30 09:14:44 CST 2008
不错

这个文章共有 1 条评论
主题: XML+正则表达式+反射+脚本引擎 实现简单的业务可配 上一篇文章
返回文章列表 返回〔J2SE综合〕
下一篇文章 主题: Mustang与Rhino:Java 6中的脚本编写


文字广告链接
        自主、快速定制基于JAVA的B/S业务系统          重量级企业在线自定义WEB报表平台
        Excel制表、零代码发布、打印、图表结合——快逸报表,免费、稳定、功能强大的java工具
        技术圈: 关于Java、dotNet、PHP、Ruby、奇客、Web2.0等更多资讯博客精选文章

关于 JR  |  版权声明  |  联系我们 

©2002-2006 JR 版权所有 沪ICP备05019622号