Java BigInteger Parse toBigIntegerString(byte[] bytes, boolean signed, boolean littleEndian)

Here you can find the source of toBigIntegerString(byte[] bytes, boolean signed, boolean littleEndian)

Description

Convert bytes to big integer number String

License

Apache License

Declaration

public static String toBigIntegerString(byte[] bytes, boolean signed, boolean littleEndian) 

Method Source Code

//package com.java2s;
/*//  w  w  w . j  a va 2s.c  om
 * Copyright 2012-2014 Netherlands eScience Center.
 *
 * 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 the following location:
 *
 *     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.
 * 
 * For the full license, see: LICENSE.txt (located in the root folder of this distribution).
 * ---
 */

import java.math.BigInteger;

public class Main {
    /** Convert bytes to big integer number String */
    public static String toBigIntegerString(byte[] bytes, boolean signed, boolean littleEndian) {
        if (bytes == null)
            return null;

        if (bytes.length <= 0)
            return "";
        int len = bytes.length;

        if (littleEndian) {
            for (int i = 0; i < len / 2; i++) {
                byte b = bytes[i];
                bytes[i] = bytes[len - i - 1];
                bytes[len - i - 1] = b;
            }
        }

        if (signed == false) {
            // pad zero byte to big endian array:
            byte newbytes[] = new byte[len + 1];
            newbytes[0] = 0;
            for (int i = 0; i < len; i++)
                newbytes[i + 1] = bytes[i];
            bytes = newbytes;
        }

        return new BigInteger(bytes).toString(10); // base 10
    }

    /** Null Pointer Safe toString() method */
    public static String toString(Object obj) {
        if (obj == null)
            return "<NULL>";
        else
            return obj.toString();
    }
}

Related

  1. toBigInteger(String bigInteger)
  2. toBigInteger(String s)
  3. ToBigInteger(String s)
  4. toBigInteger(String text)
  5. toBigInteger(String value, BigInteger defaultValue)