Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LICENSOR BE

import java.math.BigInteger;

public class Main {
    /**
     * Convert a BigInteger to a byte-array, but treat the byte-array given from
     * the BigInteger as unsigned and removing any leading zero bytes; e.g. a
     * 1024 bit integer with its highest bit set will result in an 128 byte
     * array.
     *
     * @param bigInteger The BigInteger to convert.
     * @return The byte-array representation of the BigInterger without
     *         signum-bit. null, if the BigInteger is null.
     * @preconditions
     * @postconditions
     */
    public static byte[] unsignedBigIntergerToByteArray(BigInteger bigInteger) {
        if (bigInteger == null) {
            return null;
        }
        byte[] integerBytes = bigInteger.toByteArray();
        byte[] unsignedIntegerBytes;
        if ((integerBytes.length > 0) && (integerBytes[0] == 0x00)) {
            unsignedIntegerBytes = new byte[integerBytes.length - 1];
            for (int i = 0; i < unsignedIntegerBytes.length; i++) {
                unsignedIntegerBytes[i] = integerBytes[i + 1];
            }
        } else {
            unsignedIntegerBytes = integerBytes;
        }

        return unsignedIntegerBytes;
    }
}