Java BigInteger to bigIntegerToList(BigInteger number)

Here you can find the source of bigIntegerToList(BigInteger number)

Description

Transforms a BigInteger number to a List of Integer .

License

Apache License

Parameter

Parameter Description
number a parameter

Declaration

public static List<Integer> bigIntegerToList(BigInteger number) 

Method Source Code


//package com.java2s;
/*//from   w  w w .  ja  va 2  s . com
 * Copyright 2015-2016 USEF Foundation
 *
 * 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.
 */

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * Transforms a {@link BigInteger} number to a List of {@link Integer}.
     * <p/>
     * Example: the BigInteger 68 is transformed to its binary representation <code>1000100</code> witch is transformed to a list
     * with integers 3 and 7, since only the 3rd and 7th bit are true.
     *
     * @param number
     * @return
     */
    public static List<Integer> bigIntegerToList(BigInteger number) {
        if (number == null) {
            return new ArrayList<>(0);
        }
        String bitArray = number.toString(2);
        List<Integer> result = new ArrayList<>();
        int length = bitArray.length();
        for (int index = length - 1; index >= 0; --index) {
            if (bitArray.charAt(index) == '1') {
                result.add(length - index);
            }
        }
        return result;
    }
}

Related

  1. bigIntegerToBytes(BigInteger n, byte[] data, int[] offset)
  2. bigIntegerToBytes(final BigInteger bigInteger)
  3. bigIntegerToDigits(BigInteger n)
  4. BigIntegerToEightBytes(BigInteger value)
  5. bigIntegerToHex(final BigInteger bigInteger)
  6. bigIntegerToString(final BigInteger value)
  7. bigIntegerToUnsignedByteArray(BigInteger a, int len)
  8. bigIntToArray(BigInteger data)
  9. bigIntToHash(BigInteger keyValue)