How to Convert BigInteger to String?

How to Convert BigInteger to String?

Coversion of BigInteger to String is one of the day to day task for java programmer.
Few days back i have the need to conver the BigInteger to Integer in my programming
It is simple task to do the same

BigInteger is commonly used for storing the numerica values by result of arbituary arthamatic calculations.
In My Previous Post, we have discussed about the conversion of BigInteger to Integer or viceverse. we can also do the same for conversiton to String

We have two cases 1. BigInteger to String 2. String to BigInteger

Covert BigInteger to String object:-


We can do this in many wasy

One of the way is to use the BigInteger.toString() method or String.valueOf() in java.This is simple coding task to do.

BigInteger class constructor for passing String as parameter for BigInteger object creation

BigInteger bi=new BigInteger("123");

As you know every java class has toString method, To return the String object with for the BigInteger class.

Following is the line of code for returning String object

String str=bi.toString();

//The following case works without throwing exception
  BigInteger bigIntegerdemo = new BigInteger("123");
  System.out.println(bigIntegerdemo.toString());

  //The following case doest not works and throws NumberFormatException
  BigInteger bigIntegerdemo1 = new BigInteger("123");
  System.out.println(bigIntegerdemo1.toString());


The disadvatanges with this approach is alwasy we need to pass the numeric value in the form of String for BigInteger constructor.
if String like "abc" is passed, it throws NumberFormatException.


The other way around to conversion to String is first convert BigInteger object to bytes using toByteArray() method and then passing array bytes to String's byte Constructor

BigInteger bigIntegerdemo2 = new BigInteger("123");
  byte b[]=bigIntegerdemo2.toByteArray();
  String s=new String(b);

k


Covert String to BigInteger object:-

One way

Other way around is to use the BigInteger.toByteArray()
In this process, we are converting String to array of bytes using String.getBytes() method and then Bytes to BigInteger using toByteArray() method.
String msg = new String ("Hi Techrocksz!");
  BigInteger bi = new BigInteger(msg.getBytes());
  System.out.println(new String(bi.toByteArray())); // prints "Hi Techrocksz!"

Hoep you understand this. Please leave a comment if there are any more ways to convert this

1 comment:

  1. //The following case works without throwing exception
    BigInteger bigIntegerdemo = new BigInteger("123");
    System.out.println(bigIntegerdemo.toString());

    //The following case doest not works and throws NumberFormatException
    BigInteger bigIntegerdemo1 = new BigInteger("123");
    System.out.println(bigIntegerdemo1.toString());

    ---------------
    I could not find any difference 2 blocks of code.
    Please could you clarify this.

    ReplyDelete

Note: only a member of this blog may post a comment.