Example usage for java.math BigInteger toByteArray

List of usage examples for java.math BigInteger toByteArray

Introduction

In this page you can find the example usage for java.math BigInteger toByteArray.

Prototype

public byte[] toByteArray() 

Source Link

Document

Returns a byte array containing the two's-complement representation of this BigInteger.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    byte[] bytes = new byte[] { 0x1, 0x00, 0x00 };
    BigInteger bi = new BigInteger(bytes);
    bytes = bi.toByteArray();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    BigInteger bi = new BigInteger("100100100111111110000", 2);

    byte[] bytes = bi.toByteArray();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    BigInteger bi = new BigInteger("100101000111111110000", 2);

    byte[] bytes = bi.toByteArray();
}

From source file:Main.java

public static void main(String[] args) {

    // create and assign value to byte array b3
    byte b3[] = { 0x1, 0x00, 0x00 };

    BigInteger bi1 = new BigInteger("10");
    BigInteger bi2 = new BigInteger(b3); // using byte[] constructor of
                                         // BigInteger

    // assign byte array representation of bi1, bi2 to b1, b2
    byte[] b1 = bi1.toByteArray();
    byte[] b2 = bi2.toByteArray();

    for (int i = 0; i < b1.length; i++) {
        System.out.format("0x%02X\n", b1[i]);
    }//from  w w  w .j  a v a  2  s  .  co m
    // print byte array b2 using for loop
    for (int j = 0; j < b2.length; j++) {
        System.out.format("0x%02X ", b2[j]);
    }
}

From source file:RSA.java

/** Trivial test program. */
public static void main(String[] args) {
    RSA rsa = new RSA(1024);

    String text1 = "Yellow and Black Border Collies";
    System.out.println("Plaintext: " + text1);
    BigInteger plaintext = new BigInteger(text1.getBytes());

    BigInteger ciphertext = rsa.encrypt(plaintext);
    System.out.println("Ciphertext: " + ciphertext);
    plaintext = rsa.decrypt(ciphertext);

    String text2 = new String(plaintext.toByteArray());
    System.out.println("Plaintext: " + text2);
}

From source file:Main.java

public static String encodeBigInteger(BigInteger v) {
    return bytesToHex(v.toByteArray());
}

From source file:Main.java

public static byte[] BigIntegerToByteArrayWithoutSign(BigInteger value) {
    byte[] bytes = value.toByteArray();
    if (bytes[0] == (byte) 0) {
        bytes = copyArray(bytes, 1, bytes.length - 1);
    }/*  w  w w  .  j a  v  a2  s . co m*/
    return bytes;
}

From source file:Main.java

public static byte[] convertIntToByteArray(int arg) {
    BigInteger bigInt = BigInteger.valueOf(arg);
    return bigInt.toByteArray();
}

From source file:Main.java

private static byte[] getPadded(BigInteger n, int length) {
    byte[] bs = n.toByteArray();
    if (bs.length < length) {
        byte[] tmp = new byte[length];
        System.arraycopy(bs, 0, tmp, length - bs.length, bs.length);
        bs = tmp;/*from   ww w . ja  v  a2s . com*/
    } else if (bs.length > length) { //BigInteger bigger (probably due to 0 padding)
        byte[] tmp = new byte[length];
        System.arraycopy(bs, bs.length - length, tmp, 0, length);
        bs = tmp;
    }
    return bs;
}

From source file:Main.java

/**
 * Return the value of <tt>big</tt> as a byte array. Although BigInteger
 * has such a method, it uses an extra bit to indicate the sign of the
 * number. For elliptic curve cryptography, the numbers usually are
 * positive. Thus, this helper method returns a byte array of minimal
 * length, ignoring the sign of the number.
 *
 * @param value the <tt>BigInteger</tt> value to be converted to a byte
 *              array//from   w  w  w. j a v a  2s .c  o  m
 * @return the value <tt>big</tt> as byte array
 */
public static byte[] toMinimalByteArray(BigInteger value) {
    byte[] valBytes = value.toByteArray();
    if ((valBytes.length == 1) || (value.bitLength() & 0x07) != 0) {
        return valBytes;
    }
    byte[] result = new byte[value.bitLength() >> 3];
    System.arraycopy(valBytes, 1, result, 0, result.length);
    return result;
}