我写的代码。不过现在还有问题,高手指点long p = new Random().nextLong(); System.out.println(p); byte[] a = new byte[8]; for(int i=0;i<8;i++) { a[i] = (byte) (p%(256)); p = p>>>8; } System.out.println(new String(a)); long pp=0;
有没有API啊,我没有找到,高手指点啊 上面的比较的时候有问题,这会改好的了 long p = new Random().nextLong(); long p2= p; System.out.println(p); byte[] a = new byte[8]; for(int i=0;i<8;i++) { a[i] = (byte) (p%(256)); p = p>>>8; }
public static long readLong(byte[] b, int index) { long r = 0; for (int i = index; i < b.length; i++) { r <<= 8; r |= (b[i] & 0x00000000000000ff); } return r; }
public static byte[] longToBytes(long n) { byte[] buf = new byte[8]; for (int i = buf.length - 1; i >= 0; i--) { buf[i] = (byte) (n & 0x00000000000000ff); n >>= 8; } return buf; }
waterborn(WaterBorn)有点问题哦: public static long readLong(byte[] b, int index) { long r = 0; for (int i = index; i < b.length; i++) { r <<= 8; r += (b[i] & 0x00000000000000ff);//累加 } return r; }
public static byte[] longToBytes(long n) { byte[] buf = new byte[8]; for (int i = buf.length - 1; i >= 0; i--) { buf[i] = (byte) (n & 0x00000000000000ff); n >>>= 8;//无符号左移会不会保险些 } return buf; }
System.out.println(p);
byte[] a = new byte[8];
for(int i=0;i<8;i++)
{
a[i] = (byte) (p%(256));
p = p>>>8;
}
System.out.println(new String(a));
long pp=0;
for(int i=7;i>=0;i--)
{
pp = pp+a[i];
pp = pp<<8;
}
System.out.println(pp);
System.out.println(pp==p);
上面的比较的时候有问题,这会改好的了
long p = new Random().nextLong();
long p2= p;
System.out.println(p);
byte[] a = new byte[8];
for(int i=0;i<8;i++)
{
a[i] = (byte) (p%(256));
p = p>>>8;
}
//System.out.println(new String(a));
long pp=0;
for(int i=7;i>=0;i--)
{
pp = pp+a[i];
pp = pp<<8;
}
System.out.println(pp);
System.out.println(pp==p2);
long r = 0;
for (int i = index; i < b.length; i++) {
r <<= 8;
r |= (b[i] & 0x00000000000000ff);
}
return r;
}
public static byte[] longToBytes(long n) {
byte[] buf = new byte[8];
for (int i = buf.length - 1; i >= 0; i--) {
buf[i] = (byte) (n & 0x00000000000000ff);
n >>= 8;
}
return buf;
}
Long.parseLong(String);Long.toString(long i)=string;
public static long readLong(byte[] b, int index) {
long r = 0;
for (int i = index; i < b.length; i++) {
r <<= 8;
r += (b[i] & 0x00000000000000ff);//累加
}
return r;
}
public static byte[] longToBytes(long n) {
byte[] buf = new byte[8];
for (int i = buf.length - 1; i >= 0; i--) {
buf[i] = (byte) (n & 0x00000000000000ff);
n >>>= 8;//无符号左移会不会保险些
}
return buf;
}
pp = pp<<8;
应该先移位后加余数;
但是你没有考虑到byte里的符号位。比如11000000无符号是192,byte中是-64