Get BigInteger from an object : BigInteger « Data Type « Java Tutorial






import java.math.BigDecimal;
import java.math.BigInteger;

/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */



/**
 * Operations on <code>Object</code>.
 * 
 * This class tries to handle <code>null</code> input gracefully.
 * An exception will generally not be thrown for a <code>null</code> input.
 * Each method documents its behaviour in more detail.
 *
 * @author <a href="mailto:nissim@nksystems.com">Nissim Karpenstein</a>
 * @author <a href="mailto:janekdb@yahoo.co.uk">Janek Bogucki</a>
 * @author Daniel L. Rall
 * @author Stephen Colebourne
 * @author Gary Gregory
 * @author Mario Winterer
 * @author <a href="mailto:david@davidkarlsen.com">David J. M. Karlsen</a>
 * @since 1.0
 * @version $Id: ObjectUtils.java 594336 2007-11-12 22:54:02Z bayard $
 */
public class Main {
  public static BigInteger getBigInteger(Object value) {
    BigInteger ret = null;
    if ( value != null ) {
        if ( value instanceof BigInteger ) {
            ret = (BigInteger) value;
        } else if ( value instanceof String ) {
            ret = new BigInteger( (String) value );
        } else if ( value instanceof BigDecimal ) {
            ret = ((BigDecimal) value).toBigInteger();
        } else if ( value instanceof Number ) {
            ret = BigInteger.valueOf( ((Number) value).longValue() );
        } else {
            throw new ClassCastException( "Not possible to coerce [" + value + "] from class " + value.getClass() + " into a BigInteger." );
        }
    }
    return ret;
}
}








2.45.BigInteger
2.45.1.Create BigInteger via string
2.45.2.Create BigInteger via long type variable
2.45.3.Create BigInteger from byte array
2.45.4.Operating with Big Integer Values
2.45.5.Multiply one BigInteger to another BigInteger
2.45.6.Subtract one BigInteger from another BigInteger
2.45.7.Divide one BigInteger from another BigInteger
2.45.8.Negate a BigInteger
2.45.9.Calculate the power on a BigInteger
2.45.10.Performing Bitwise Operations with BigInteger
2.45.11.Get the value of a bit
2.45.12.Set a bit for BigInteger
2.45.13.Clear a bit in a BigInteger
2.45.14.Flip a bit in a BigInteger
2.45.15.Shift the bits in a BigInteger
2.45.16.Shift right in a BigInteger
2.45.17.xor a BigInteger
2.45.18.and operation on BigInteger
2.45.19.not operation for BigInteger
2.45.20.or operation for BigInteger
2.45.21.antNot operation on BigInteger
2.45.22.Retrieve the current bits in a byte array in twos-complement form.
2.45.23.Parse octal string to create BigInteger
2.45.24.Parse decimal string to create BigInteger
2.45.25.Parse hexadecimal string to create BigInteger
2.45.26.Parsing and Formatting a Big Integer into Binary
2.45.27.Parsing and Formatting a Big Integer into octal
2.45.28.Parsing and Formatting a Big Integer into decimal
2.45.29.Parsing and Formatting a Byte Array into Binary
2.45.30.Parsing and Formatting a Byte Array into Octal
2.45.31.Parsing and Formatting a Byte Array into decimal
2.45.32.Parsing and Formatting a Byte Array into Hexadecimal
2.45.33.Parse and format to hexadecimal
2.45.34.Parse and format to arbitrary radix <= Character.MAX_RADIX
2.45.35.Parse binary string
2.45.36.Convert BigInteger into another radix number
2.45.37.Do math operation for BigInteger
2.45.38.Operate with big integer values in code
2.45.39.Get byte array from BigInteger
2.45.40.BigInteger.isProbablePrime
2.45.41.performing the modular exponential
2.45.42.Use BigInteger
2.45.43.Get BigInteger from an object