Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting BigInteger to binary string

Tags:

Can we convert Biginteger to binary string

   String s1 = "0011111111101111111111111100101101111100110000001011111000010100";    String s2 = "0011111111100000110011001100110011001100110011001100110011001100";    BigInteger bi1, bi2, bi3;    bi1 = new BigInteger(s1,2);    bi2 = new BigInteger(s2,2);    bi3 = bi1.xor(bi2); 

How to convert bi3 to binary string

like image 422
Kailash Avatar asked Dec 24 '13 13:12

Kailash


People also ask

How do I change BigInteger to string?

BigInteger. toString() method returns the decimal String representation of this BigInteger. This method is useful to convert BigInteger to String. One can apply all string operation on BigInteger after applying toString() on BigInteger.

How do you parse a BigInteger?

Firstly, take two BigInteger objects and set values. BigInteger one, two; one = new BigInteger("99"); two = new BigInteger("978"); Now, parse BigInteger object “two” into Binary.

How do you convert BigInteger to hexadecimal?

readLine(); BigInteger toHex=new BigInteger(dec,16); String s=toHex. toString(16); System. out. println("The value in Hex is: "+ s);

How do I convert BigInteger to integer?

math. BigInteger. intValue() converts this BigInteger to an integer value. If the value returned by this function is too big to fit into integer value, then it will return only the low-order 32 bits.


2 Answers

You can use toString(radix) for that:

String s3 = bi3.toString(2); 
like image 67
Sergey Kalinichenko Avatar answered Oct 19 '22 03:10

Sergey Kalinichenko


import java.math.BigInteger; import java.util.Scanner;

public static void main(String[] args) {     Scanner in = new Scanner(System.in);     System.out.println("Enter a Number: ");     String n = in.next();     BigInteger nn = new BigInteger(n);     if(nn.compareTo(BigInteger.ZERO)<0){         System.out.println("Number cannot be less than 0");     }else{         System.out.println("Convert to binary is:");         print2Binaryform(nn);         System.out.println("");     } } private static void print2Binaryform(BigInteger number) {     BigInteger reminder2;     if(number.compareTo(BigInteger.ONE)<=0){         System.out.print(number);         return;     }     reminder2 = number.mod(new BigInteger(""+2));     print2Binaryform(new BigInteger(""+number.divide(new BigInteger("2"))));     System.out.print(reminder2); } 
like image 45
Riven Flows Avatar answered Oct 19 '22 03:10

Riven Flows