Java BigInteger From bytesToBigInteger(byte[] buffer)

Here you can find the source of bytesToBigInteger(byte[] buffer)

Description

This function converts the bytes in a byte array to its corresponding big integer value.

License

Open Source License

Parameter

Parameter Description
buffer The byte array containing the big integer.

Return

The corresponding big integer value.

Declaration

static public BigInteger bytesToBigInteger(byte[] buffer) 

Method Source Code

//package com.java2s;
/************************************************************************
 * Copyright (c) Crater Dog Technologies(TM).  All Rights Reserved.     *
 ************************************************************************
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.        *
 *                                                                      *
 * This code is free software; you can redistribute it and/or modify it *
 * under the terms of The MIT License (MIT), as published by the Open   *
 * Source Initiative. (See http://opensource.org/licenses/MIT)          *
 ************************************************************************/

import java.math.*;

public class Main {
    /**/*from  www .j a  va 2  s  . c o m*/
     * This function converts the bytes in a byte array to its corresponding big integer value.
     *
     * @param buffer The byte array containing the big integer.
     * @return The corresponding big integer value.
     */
    static public BigInteger bytesToBigInteger(byte[] buffer) {
        return new BigInteger(buffer);
    }

    /**
     * This function converts the bytes in a byte array at the specified index to its
     * corresponding big integer value.
     *
     * @param buffer The byte array containing the big integer.
     * @param index The index for the first byte in the byte array.
     * @return The corresponding big integer value.
     */
    static public BigInteger bytesToBigInteger(byte[] buffer, int index) {
        int length = bytesToInt(buffer, index); // pull out the length of the big integer
        index += 4;
        byte[] bytes = new byte[length];
        System.arraycopy(buffer, index, bytes, 0, length); // pull out the bytes for the big integer
        return new BigInteger(bytes);
    }

    /**
     * This function converts the bytes in a byte array to its corresponding integer value.
     *
     * @param buffer The byte array containing the integer.
     * @return The corresponding integer value.
     */
    static public int bytesToInt(byte[] buffer) {
        return bytesToInt(buffer, 0);
    }

    /**
     * This function converts the bytes in a byte array at the specified index to its
     * corresponding integer value.
     *
     * @param buffer The byte array containing the integer.
     * @param index The index for the first byte in the byte array.
     * @return The corresponding integer value.
     */
    static public int bytesToInt(byte[] buffer, int index) {
        int length = buffer.length - index;
        if (length > 4)
            length = 4;
        int integer = 0;
        for (int i = 0; i < length; i++) {
            integer |= ((buffer[index + length - i - 1] & 0xFF) << (i * 8));
        }
        return integer;
    }
}

Related

  1. byteArrayToBigInteger(final byte[] data)
  2. ByteArrayToBigIntegerWithoutSign(byte[] array)
  3. bytesToBigInteger(byte[] data)
  4. bytesToBigInteger(byte[] data, int[] offset)
  5. convertToBigInteger(String s)
  6. convertToBigInteger(String sequence)