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

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;
 }

……

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值