Java Conversion between String and BigInteger Tutorial

In the previous tutorial, we have seen Java Conversion between BigDecimal and String Tutorial. In this tutorial, we will learn how to convert String to BigInteger and also we will convert BigInterger to String back.

In this tutorial, we create StringConverter<> generic interface with two generic methods. This interface can be used to convert any wrapper classes (Integer, Short, Double, etc) to String and vice-versa.
  • toString(T object) - Converts the object provided into its string form. 
  • T fromString(String string) - Converts the string provided into an object defined by the specific converter.

StringConverter.java

Here we create a generic StringConverter<> interface. This interface declares the following generic methods. 
  • toString(T object) - Converts the object provided into its string form. 
  • T fromString(String string) - Converts the string provided into an object defined by the specific converter.
package net.javaguides.string.conversion;

/**
 * Converter defines conversion behavior between strings and objects.
 * The type of objects and formats of strings are defined by the subclasses
 * of Converter.
 */
public interface StringConverter<T> {
    /**
    * Converts the object provided into its string form.
    * @return a string representation of the object passed in.
    */
    public abstract String toString(T object);

    /**
    * Converts the string provided into an object defined by the specific converter.
    * @return an object representation of the string passed in.
    */
    public abstract T fromString(String string);
}

BigIntegerStringConverter.java

Let's create a BigIntegerStringConverter class which converts String to BigInteger and also we will convert BigInterger to String back.
package net.javaguides.string.conversion;

import java.math.BigInteger;

/**
 * <p>
 * {@link StringConverter} implementation for {@link BigInteger} values.
 * </p>
 */
public class BigIntegerStringConverter implements StringConverter < BigInteger > {
    @Override
    public BigInteger fromString(String value) {
        // If the specified value is null or zero-length, return null
        if (value == null) {
            return null;
        }

        value = value.trim();

        if (value.length() < 1) {
            return null;
        }

        return new BigInteger(value);
    }

    @Override
    public String toString(BigInteger value) {
        // If the specified value is null, return a zero-length String
        if (value == null) {
            return "";
        }

        return ((BigInteger) value).toString();
    }

    public static void main(String[] args) {

        String string = "100";
        BigIntegerStringConverter bigIntegerStringConverter = new BigIntegerStringConverter();
        BigInteger bigInteger = bigIntegerStringConverter.fromString(string);
        System.out.println("String to bigInteger -> " + bigInteger);

        String bigIntstr = bigIntegerStringConverter.toString(bigInteger);
        System.out.println("bigInteger to string -> " + bigIntstr);

    }
}
Output:
String to bigInteger -> 100
bigInteger to string -> 100
You can use this StringConverter and BigIntegerStringConverter as a utility in your projects.

Comments