Java BigInteger Calculate intValue(BigInteger bi)

Here you can find the source of intValue(BigInteger bi)

Description

Converts a BigInteger to an integer, if the BigInteger is in the correct range.

License

Apache License

Parameter

Parameter Description
bi The BigInteger to convert.

Return

The integer value.

Declaration

public static int intValue(BigInteger bi) 

Method Source Code

//package com.java2s;
/***********************************************************
 * $Id$//from w w w  .  j  a  va2 s .  c om
 * 
 * PKCS#15 cryptographic provider of the opensc project.
 * http://www.opensc-project.org
 *
 * Licensed 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.
 *
 * Created: 01.01.2008
 * 
 ***********************************************************/

import java.math.BigInteger;

public class Main {
    private static BigInteger MIN_INT_BIG_INTEGER = BigInteger.valueOf(Integer.MIN_VALUE);
    private static BigInteger MAX_INT_BIG_INTEGER = BigInteger.valueOf(Integer.MAX_VALUE);

    /**
     * Converts a BigInteger to an integer, if the BigInteger is
     * in the correct range. Otherwise, an IllegalArgumentException is thrown.
     * 
     * @param bi The BigInteger to convert.
     * @return The integer value.
     */
    public static int intValue(BigInteger bi) {
        if (bi.compareTo(MIN_INT_BIG_INTEGER) < 0)
            throw new IllegalArgumentException("BigInteger [" + bi + "] is too small to convert to int.");
        if (bi.compareTo(MAX_INT_BIG_INTEGER) > 0)
            throw new IllegalArgumentException("BigInteger [" + bi + "] is too big to convert to int.");

        return bi.intValue();
    }
}

Related

  1. greater(BigInteger i1, BigInteger i2)
  2. hexEncode(BigInteger bigInteger)
  3. increment(BigInteger integer)
  4. int2zpBin(BigInteger num, int len)
  5. integerToString(BigInteger value)
  6. intValueExact(BigInteger bigint)
  7. jsonBigInteger(JsonValue value)
  8. length(BigInteger bi)
  9. listToBigInteger(List list)