您的位置:首页 > 其它

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

2008-05-03 17:22 381 查看
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;
}

……
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: