BigInteger bx=new BigInteger("3806908688");
byte x[]=new byte[4];
String s=bx.toString(10);
System.out.println("string: "+s);
x=s.getBytes();
int l=0,i=0;
l |= x[i] & 0xFF;
l <<= 8;
l |= x[i+1] & 0xFF;
l <<= 8;
l |= x[i+2] & 0xFF;
l <<= 8;
l |= x[i+3] & 0xFF;
System.out.println(l);
The output is 859320374 instead of 3806908688. Why is this happening when I’m converting a BigInteger into a byte array and printing this byte array as an integer
I also tried using bx.toByteArray() but the result is same
If you have to do it manually, do it in a loop:
Alternatively you could just use methods offered by
BigInteger: