日期:2014-05-20 浏览次数:20822 次
public static long readUnit64(ByteBuffer buf){
int l = 0;
long rst = 0L;
while(l<64){
int t = buf.get();
rst |= (long)((t & 127) << l);
if((t & 128) == 0)
return rst;
l += 7;
}
return 0;
}
public static void writeUnit64(ByteBuffer buf, long val){
while(true){
if ((0xFFFFFF80 & val) == 0L){
buf.put((byte)(int)val);
break;
}
buf.put((byte)(128 | 127 & (int)val));
val >>>= 7;
}
}
public static void main(String[] args) {
long t = System.currentTimeMillis()/1000; //问题在这里,不除以1000就是错的
System.out.println("原始数据:"+t);
ByteBuffer buf = ByteBuffer.allocate(128);
writeUnit64(buf, t);
buf.flip();
System.out.println("原始数据:"+readUnit64(buf));
}
public static long readUnit64(ByteBuffer buf){
int l = 0;
long rst = 0L;
//while(l<64){
while(buf.position()<buf.limit()){//换一下判断条件
long t = (int)buf.get();//t改成long
rst
------解决方案--------------------
= (t & 127) << l;
if((t & 128) == 0)
return rst;
l += 7;
}
return rst;//返回rst.
}