Java BigInteger From byteArrayToBigInteger(final byte[] data)

Here you can find the source of byteArrayToBigInteger(final byte[] data)

Description

Convert a BCD byte[] to BigInteger

License

Open Source License

Parameter

Parameter Description
data The byte[] to be converted to BigInteger

Exception

Parameter Description
IllegalArgumentException an exception

Return

A BigInteger built out of a BCD byte array

Declaration

public static BigInteger byteArrayToBigInteger(final byte[] data) 

Method Source Code

//package com.java2s;
/**//from  w  w w.  jav a  2 s.  c  o  m
 * ****************************************************************************
 * Copyright (c) 2015, MasterCard International Incorporated and/or its
 * affiliates. All rights reserved.
 * <p/>
 * The contents of this file may only be used subject to the MasterCard
 * Mobile Payment SDK for MCBP and/or MasterCard Mobile MPP UI SDK
 * Materials License.
 * <p/>
 * Please refer to the file LICENSE.TXT for full details.
 * <p/>
 * TO THE EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS", WITHOUT
 * WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NON INFRINGEMENT. TO THE EXTENT PERMITTED BY LAW, IN NO EVENT SHALL
 * MASTERCARD OR ITS AFFILIATES BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 * *****************************************************************************
 */

import java.math.BigInteger;

public class Main {
    /**
     * Convert a BCD byte[] to BigInteger
     *
     * @param data The byte[] to be converted to BigInteger
     *
     * @return A BigInteger built out of a BCD byte array
     *
     * @throws IllegalArgumentException
     * */
    public static BigInteger byteArrayToBigInteger(final byte[] data) {
        StringBuilder stringBuilder = new StringBuilder(data.length * 2);
        for (final byte aData : data) {
            int digit = (aData & 0xF0) >> 4;
            if (digit < 0 || digit > 9) {
                throw new IllegalArgumentException("Invalid digit: "
                        + digit);
            }
            stringBuilder.append((char) (digit + 0x30));
            digit = aData & 0x0F;
            if (digit < 0 || digit > 9) {
                throw new IllegalArgumentException("Invalid digit: "
                        + digit);
            }
            stringBuilder.append((char) (digit + 0x30));
        }
        return new BigInteger(stringBuilder.toString(), 10);
    }
}

Related

  1. ByteArrayToBigIntegerWithoutSign(byte[] array)
  2. bytesToBigInteger(byte[] buffer)
  3. bytesToBigInteger(byte[] data)
  4. bytesToBigInteger(byte[] data, int[] offset)