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

上一篇 / 下一篇  2008-01-04 14:39:00 / 个人分类:开发人生

 

我们如果多了解一些底层东西,对开发程序是很有帮助的.
   我把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. }

TAG:

headhunter 引用 删除 headhunter   /   2008-01-04 19:02:00

i am a headhunter and now i am searching an IT operation Mgr who should has experience in retail industry or supermarket, this position will cover all china, another position is for a senior programmer who should be familiar with java and oracle, and should has experience in insurance corp, third position is logistic Svr who shoud be female and Hukou is in Sahnghai.these positions all require fluent written and spoken English.and all for  corps of fortune 500 and will base in HQ(shanghai), if you are interested pls contact 13701017004,or


freeshi2007@hotmail.com


thanks yours sincerely vincent

headhunter 引用 删除 headhunter   /   2008-01-04 19:01:00

i am a headhunter and now i am searching an IT operation Mgr who should has experience in retail industry or supermarket, this position will cover all china, another position is for a senior programmer who should be familiar with java and oracle, and should has experience in insurance corp, third position is logistic Svr who shoud be female and Hukou is in Sahnghai.these positions all require fluent written and spoken English.and all for  corps of fortune 500 and will base in HQ(shanghai), if you are interested pls contact 13701017004,or


freeshi2007@hotmail.com


thanks yours sincerely vincent


 

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

日历

« 2008-10-12  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 4399
  • 日志数: 126
  • 建立时间: 2007-03-21
  • 更新时间: 2008-04-07

RSS订阅

Open Toolbar