Cryptography Reference
In-Depth Information
E
XAMPLE
.
An example of how this method may be called follows:
BigInteger bigBoy=new BigInteger(
“
255
”
);
System.out.println(bigBoy.toString());
However, it is often not necessary to convert BigIntegers to Strings before printing them,
for we can use implicit calls to a toString() method by concatenating a BigInteger to a String
using the
“
+
”
concatenation operator:
BigInteger bigBoy=new BigInteger(
“
255
”
);
System.out.println(
“
This # is:
“
+bigBoy);
The BigInteger will be displayed in base 10.
•
public byte[] toByteArray()
This method converts a BigInteger into a raw array of bytes. In effect, it returns the
two
'
s
-
complement binary representation of
this
number in the array, which is big-endian
(that is, the most significant byte is in the [0] position). The array contains the minimum num-
ber of bytes required to represent the number. For example, suppose
b
is a BigInteger hav-
ing the value 987654321=111010110111100110100010110001
base 2
. (You may wish to verify
that this is actually the base 2 representation of 987654321.) Then we make the following
call:
byte[] a=b.toByteArray();
This will produce a byteArray of 4 bytes, assign it to
, and the most significant digits
a
of the BigInteger
will be first. That is,
b
a[0]=00111010,
a[1]=11011110,
a[2]=01101000, and
a[3]=10110001.
This representation is compatible with the (byte[]) constructor.
•
public int intValue()
This method converts
this
number to an int.
•
public long longValue()
This method converts
this
number to a long.
•
public float floatValue()
number to a float. It is similar to the double-to-float narrow-
ing primitive conversion defined in The Java Language Specification: If the number has
too great a magnitude to represent as a float, it will be converted to infinity or negative
infinity as appropriate.
This method converts
this
Search WWH ::
Custom Search