NOTE: We are going to use the UInt256
class for all examples, but the same behaviors are possible with the UInt384
, UInt64
, UInt32
classes.
We refer to those classes as UInt
.
Creating Uints
valueOf
You can initialize a UInt
with the static method valueOf
, with an integer, a long, or a BigInteger object. This only accepts positive values.
// from an int
UInt256 value = UInt256.valueOf(42);
// from a long
UInt256 value = UInt256.valueOf(42L);
// from a BigInteger
UInt256 value = UInt256.valueOf(BigInteger.ONE);
fromBytes
You can initialize a UInt
from a Bytes
object, using the fromBytes
method.
UInt256 value = UInt256.fromBytes(Bytes.wrap(new byte[] {0x01, 0x02, 0x03}));
fromHexString
You can initialize a UInt
from a string representing a hexadecimal encoding of bytes, with an optional prefix of 0x
.
UInt256 value = UInt256.fromHexString("0xdeadbeef");