banner



How To Convert String To Biginteger In Java

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 convertString to BigInteger and also we will convert BigInterger toString 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 genericStringConverter<> 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 convertsString to BigInteger and also we will convert BigInterger toString 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.

Core Java String Handling

How To Convert String To Biginteger In Java

Source: https://www.javaguides.net/2019/07/java-conversion-between-string-andbiginteger-tutorial.html

Posted by: wilkinsontherob39.blogspot.com

0 Response to "How To Convert String To Biginteger In Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel