0
BigInteger to unsigned binary string
I'm programming a binary string to string converter app. Binary string to string works fine but vice versa i have problems with chars with high ascii values. I use string.getbytes to get a byte array representing the string. then I generate a BigInteger out of it. i convert this big integer to a binary (or hex) string using bigInteger.toString (2). the problem is negative bigintegers get a - befor them instead of being converted completet to 1 & 0. Can you make a biginteger unsigned or get rid of the - ?
4 ответов
0
it is because the type you are using is too small, the big int would see 11 0000 0100101100 111 as a whole number, so if you go vice versa, you might want to use a Double or a string instead
0
But what can I do if I want to convert a very long String that is too big for double.
Biginteger is just working perfectly for me excep for the minus as I can set the radix for the toString as I want.
0
You could parse it as a string, the minus issue will not happen this eay.
0
After a few hours of google I found the solution:
public static String convertBytesToBinaryString(byte[] bytes, int radix) {
BigInteger bigInteger = new BigInteger(bytes);
if(bigInteger.compareTo(BigInteger.ZERO)<0){
bigInteger=new BigInteger(1,bytes);
}
return bigInteger.toString(radix);
}
surprisingly it worked!
Thank you anyways