neodatis.odb 第二篇 一切都是比特

来源:互联网 发布:网络棋牌赌博没人管 编辑:程序博客网 时间:2024/06/06 07:01

org.neodatis.odb.core.io.ByteArrayConverter

这个类的作用是 :Converts array of bytes into native objects and native objects into array of bytes

public class ByteArrayConverter {
 ……

 /**
  * bit 二进制位(或称比特) byte 字节 1Byte=8bit kilobyte(kb) 千字节 megabyte(mb) 兆字节
  * 1Mb=1024KB=1024*1024Byte gigabyte(gb) 吉字节 terabyte(tb) 太字节 PetaByte (PB)
  * ExaByte (EB) ZetaByte (ZB) YottaByte (YB) NonaByte (NB) DoggaByte (DB)
  *
  * byte 字节型 1个字节 -128-127 short 短整型 2个字节 -2的15次幂-2的15次幂-1 int 整型 4个字节
  * -2的31次幂-2的31次幂-1 long 长整型 8个字节 -2的63次幂~2的63次幂-1
  *
  * @param s
  * @return 先低后高
  */
 public static byte[] shortToByteArray(short s) {
  byte b[] = new byte[2];
  int i, shift;
  for (i = 0, shift = 8; i < 2; i++, shift -= 8) {
   b[i] = (byte) (0xFF & (s >> shift));//&0000 0000 1111 1111 先取低位,后取高位
  }
  return b;
 }

 public static short byteArrayToShort(byte[] bytes) {
  short result = 0;

  for (int i = 0; i < 2; i++) {
   result <<= 8; // left shift out the last byte
   result |= bytes[i] & 0xFF; // OR in the new byte 将高低位附加在一起
  }
  bytes = null;
  return result;
 }

……

原创粉丝点击