Search Posts

Convert 64 bits binary string to BigInteger and maintain the sign

The first and third method converted successfully with sign. The second one fail. The reason 3rd way working is : It convert to biginteger and convert to long (then we have sign),then convert back to biginteger (also with sign)

		BigInteger i = BigInteger.valueOf(0xfffffffffffffff7l);
		System.out.println(i);
		System.out.println(i.toString(16));
		System.out.println(String.valueOf(i.toString(16)));

		BigInteger i2 = new BigInteger("1111111111111111111111111111111111111111111111111111111111110111", 2);
		System.out.println(i2);
		System.out.println(i2.toString(16));
		System.out.println(String.valueOf(i2.toString(16)));

		BigInteger i3 = BigInteger.valueOf(new BigInteger("1111111111111111111111111111111111111111111111111111111111110111", 2).longValue());
		System.out.println(i3);
		System.out.println(i3.toString(16));
		System.out.println(String.valueOf(i3.toString(16)));

Leave a Reply

Your email address will not be published. Required fields are marked *