From Evernote: |
Long2BigIntegerTest |
import java.math.BigInteger;
public class Long2BigIntegerTest {
/**
* @param args
*/
public static void main(String[] args) {
System. out.println("----------------1------------------" );
long ii = -1L;
System. out.println("source:[" + ii + "]" );
byte buf[] = longToBytes(ii);
BigInteger bi = new BigInteger(1,buf);
System. out.println("unsign BigInteger : [" + bi + "]") ;
bi = new BigInteger(-1,buf);
System. out.println("sign BigInteger : [" + bi + "]") ;
long rr = readLong(buf, 0);
System. out.println(" readLong : [" + rr + "]") ;
System. out.println("-----------------------------------" );
System. out.println("----------------100-----------------" );
ii = -100L;
System. out.println("source:[" + ii + "]" );
buf = longToBytes(ii);
bi = new BigInteger(1,buf);
System. out.println("unsign BigInteger : [" + bi + "]") ;
bi = new BigInteger(-1,buf);
System. out.println("sign BigInteger : [" + bi + "]") ;
rr = readLong(buf, 0);
System. out.println(" readLong : [" + rr + "]") ;
System. out.println("-----------------------------------" );
System. out.println("-----------------+1-----------------" );
ii = 1L;
System. out.println("source:[" + ii + "]" );
buf = longToBytes(ii);
bi = new BigInteger(1,buf);
System. out.println("unsign BigInteger : [" + bi + "]") ;
bi = new BigInteger(-1,buf);
System. out.println("sign BigInteger : [" + bi + "]") ;
rr = readLong(buf, 0);
System. out.println(" readLong : [" + rr + "]") ;
System. out.println("-----------------------------------" );
System. out.println("--------------Long.MAX_VALUE--------------" );
ii = Long. MAX_VALUE;
System. out.println("source:[" + ii + "]" );
buf = longToBytes(ii);
bi = new BigInteger(1,buf);
System. out.println("unsign BigInteger : [" + bi + "]") ;
bi = new BigInteger(-1,buf);
System. out.println("sign BigInteger : [" + bi + "]") ;
rr = readLong(buf, 0);
System. out.println(" readLong : [" + rr + "]") ;
System. out.println("-----------------------------------" );
System. out.println("--------------Long.MIN_VALUE--------------" );
ii = Long. MIN_VALUE;
System. out.println("source:[" + ii + "]" );
buf = longToBytes(ii);
bi = new BigInteger(1,buf);
System. out.println("unsign BigInteger : [" + bi + "]") ;
bi = new BigInteger(-1,buf);
System. out.println("sign BigInteger : [" + bi + "]") ;
rr = readLong(buf, 0);
System. out.println(" readLong : [" + rr + "]") ;
System. out.println("-----------------------------------" );
System. out.println("--------------Long.MIN_VALUE-100--------------" );
ii = Long. MIN_VALUE;
System. out.println("source:[" + ii + "]" );
buf = longToBytes(ii);
bi = new BigInteger(1,buf);
System. out.println("unsign BigInteger : [" + bi + "]") ;
bi = new BigInteger(-1,buf);
System. out.println("sign BigInteger : [" + bi + "]") ;
rr = readLong(buf, 0);
System. out.println(" readLong : [" + rr + "]") ;
System. out.println("-----------------------------------" );
}
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;
}
}
没有评论:
发表评论