Java BigInteger Calculate writeMPI(BigInteger num, OutputStream out)

Here you can find the source of writeMPI(BigInteger num, OutputStream out)

Description

write MPI

License

GNU General Public License

Declaration

public static void writeMPI(BigInteger num, OutputStream out) throws IOException 

Method Source Code

//package com.java2s;
/* This code is part of Freenet. It is distributed under the GNU General
 * Public License, version 2 (or at your option any later version). See
 * http://www.gnu.org/ for further details of the GPL. */

import java.io.IOException;

import java.io.OutputStream;
import java.math.BigInteger;

public class Main {
    public static void writeMPI(BigInteger num, OutputStream out) throws IOException {
        out.write(MPIbytes(num));/*from  ww w. ja v a  2 s .co m*/
    }

    public static byte[] MPIbytes(BigInteger num) {
        int len = num.bitLength();
        byte[] bytes = new byte[2 + ((len + 8) >> 3)];
        System.arraycopy(num.toByteArray(), 0, bytes, 2, bytes.length - 2);
        bytes[0] = (byte) (len >> 8);
        bytes[1] = (byte) len;
        return bytes;
    }
}

Related

  1. writeBigInteger(ByteArrayOutputStream baos, BigInteger bi)
  2. writeBigInteger(ByteArrayOutputStream stream, BigInteger num)
  3. writeBigInteger(OutputStream output, BigInteger value)
  4. writeField(String tagName, BigInteger value, PrintWriter writer, int indent)
  5. writeLuposBigInteger(final BigInteger value, final int numberOfBits, final OutputStream os)