- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Parsing and Formatting a Big Integer into Binary in Java
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.
two = new BigInteger("1111010010", 2); String str = two.toString(2);
Above, we have used the following constructor. Here, radix is set as 2 for Binary for both BigInteger constructor and toString() method.
BigInteger(String val, int radix)
This constructor is used to translate the String representation of a BigInteger in the specified radix into a BigInteger.
The following is an example −
Example
import java.math.*; public class Demo { public static void main(String[] args) { BigInteger one, two; one = new BigInteger("99"); // parsing BigInteger object "two" into Binary two = new BigInteger("1111010010", 2); String str = two.toString(2); System.out.println("Result (BigInteger) : " +one); System.out.println("Result after parsing : " +str); } }
Output
Result (BigInteger) : 99 Result after parsing : 1111010010
Advertisements