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


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

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

   我们如果多了解一些底层东西,对开发程序是很有帮助的.
   我把java.nio.Bits中的代码正理了一下:

  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.         short s = -20;
  9.         byte[] b = new byte[2];
  10.         putShort(b, s, 0);
  11.         ByteBuffer buf = ByteBuffer.allocate(2);
  12.         buf.put(b);
  13.         buf.flip();
  14.         System.out.println(getShort(b, 0));
  15.         System.out.println(buf.getShort());
  16.         System.out.println("***************************");
  17.         int i = -40;
  18.         b = new byte[4];
  19.         putInt(b, i, 0);
  20.         buf = ByteBuffer.allocate(4);
  21.         buf.put(b);
  22.         buf.flip();
  23.         System.out.println(getInt(b, 0));
  24.         System.out.println(buf.getInt());
  25.         System.out.println("***************************");
  26.         long l = -40;
  27.         b = new byte[8];
  28.         putLong(b, l, 0);
  29.         buf = ByteBuffer.allocate(8);
  30.         buf.put(b);
  31.         buf.flip();
  32.         System.out.println(getLong(b, 0));
  33.         System.out.println(buf.getLong());
  34.         System.out.println("***************************");
  35.     }
  36.     public static void putShort(byte b[], short s, int index) {
  37.         b[index] = (byte) (s >> 8);
  38.         b[index + 1] = (byte) (s >> 0);
  39.     }
  40.     public static short getShort(byte[] b, int index) {
  41.         return (short) (((b[index] << 8) | b[index + 1] & 0xff));
  42.     }
  43.     // ///////////////////////////////////////////////////////
  44.     public static void putInt(byte[] bb, int x, int index) {
  45.         bb[index + 0] = (byte) (x >> 24);
  46.         bb[index + 1] = (byte) (x >> 16);
  47.         bb[index + 2] = (byte) (x >> 8);
  48.         bb[index + 3] = (byte) (x >> 0);
  49.     }
  50.     public static int getInt(byte[] bb, int index) {
  51.         return (int) ((((bb[index + 0] & 0xff) << 24)
  52.                 | ((bb[index + 1] & 0xff) << 16)
  53.                 | ((bb[index + 2] & 0xff) << 8) | ((bb[index + 3] & 0xff) << 0)));
  54.     }
  55.     // /////////////////////////////////////////////////////////
  56.     public static void putLong(byte[] bb, long x, int index) {
  57.         bb[index + 0] = (byte) (x >> 56);
  58.         bb[index + 1] = (byte) (x >> 48);
  59.         bb[index + 2] = (byte) (x >> 40);
  60.         bb[index + 3] = (byte) (x >> 32);
  61.         bb[index + 4] = (byte) (x >> 24);
  62.         bb[index + 5] = (byte) (x >> 16);
  63.         bb[index + 6] = (byte) (x >> 8);
  64.         bb[index + 7] = (byte) (x >> 0);
  65.     }
  66.     public static long getLong(byte[] bb, int index) {
  67.         return ((((long) bb[index + 0] & 0xff) << 56)
  68.                 | (((long) bb[index + 1] & 0xff) << 48)
  69.                 | (((long) bb[index + 2] & 0xff) << 40)
  70.                 | (((long) bb[index + 3] & 0xff) << 32)
  71.                 | (((long) bb[index + 4] & 0xff) << 24)
  72.                 | (((long) bb[index + 5] & 0xff) << 16)
  73.                 | (((long) bb[index + 6] & 0xff) << 8) | (((long) bb[index + 7] & 0xff) << 0));
  74.     }
  75. }


版权声明   给作者写信
本篇文章对您是否有帮助?  投票:         投票结果:     9       0
作者其它文章: 作者全部文章

这个文章共有 0 条评论
主题: Java反射经典实例 上一篇文章
返回文章列表 返回〔J2SE综合〕
下一篇文章 主题: 提高写文件的性能的一个比较简单的方法


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

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

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