Here you can find the source of bigIntegerToBytes(BigInteger integer)
Parameter | Description |
---|---|
integer | The big integer to be converted. |
static public byte[] bigIntegerToBytes(BigInteger integer)
//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 ww w .j a va 2s. c o m * This function converts a big integer to its corresponding byte array format. * * @param integer The big integer to be converted. * @return The corresponding byte array. */ static public byte[] bigIntegerToBytes(BigInteger integer) { return integer.toByteArray(); } /** * This function converts a big integer into its corresponding byte format and inserts * it into the specified buffer. * * @param integer The big integer to be converted. * @param buffer The byte array. * @return The number of bytes inserted. */ static public int bigIntegerToBytes(BigInteger integer, byte[] buffer) { return bigIntegerToBytes(integer, buffer, 0); } /** * This function converts a big integer into its corresponding byte format and inserts * it into the specified buffer at the specified index. * * @param integer The big integer to be converted. * @param buffer The byte array. * @param index The index in the array to begin inserting bytes. * @return The number of bytes inserted. */ static public int bigIntegerToBytes(BigInteger integer, byte[] buffer, int index) { int length = 4 + (integer.bitLength() + 8) / 8; System.arraycopy(intToBytes(length), 0, buffer, index, 4); // copy in the length index += 4; System.arraycopy(integer.toByteArray(), 0, buffer, index, length - 4); // copy in the big integer return length; } /** * This function converts a integer to its corresponding byte array format. * * @param i The integer to be converted. * @return The corresponding byte array. */ static public byte[] intToBytes(int i) { byte[] buffer = new byte[4]; intToBytes(i, buffer, 0); return buffer; } /** * This function converts a integer into its corresponding byte format and inserts * it into the specified buffer. * * @param i The integer to be converted. * @param buffer The byte array. * @return The number of bytes inserted. */ static public int intToBytes(int i, byte[] buffer) { return intToBytes(i, buffer, 0); } /** * This function converts a integer into its corresponding byte format and inserts * it into the specified buffer at the specified index. * * @param i The integer to be converted. * @param buffer The byte array. * @param index The index in the array to begin inserting bytes. * @return The number of bytes inserted. */ static public int intToBytes(int i, byte[] buffer, int index) { int length = buffer.length - index; if (length > 4) length = 4; for (int j = 0; j < length; j++) { buffer[index + length - j - 1] = (byte) (i >> (j * 8)); } return length; } }