Bits256
The type b256
in Fuel represents hashes and holds a 256-bit (32-bytes) value. The TypeScript SDK represents b256
as a hexlified string value for portability and provides utilities to convert to Uint8Array
when the raw bytes are required.
Generating random b256
values
To generate a random b256
value, you can use the getRandomB256()
function:
ts
See code in contextimport { getRandomB256 } from 'fuels';
// randomB256 is a hexlified string representing a 256-bit value
const randomB256: string = getRandomB256();
// 0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6
Converting between b256
and Uint8Array
To convert between a b256
hexlified string and a Uint8Array
, you can use the arrayify
and hexlify
functions:
ts
See code in contextimport { arrayify, getRandomB256, hexlify } from 'fuels';
const randomB256: string = getRandomB256();
// convert to Uint8Array
const uint8Arr = arrayify(randomB256);
// convert back to hexlified string
const hexedB256 = hexlify(uint8Arr);
Support from Address
Class
A b256
value is also supported as part of the Address
class, providing seamless integration with other components of your application. To create an Address
instance from a b256 value, use the Address.fromB256()
method:
ts
See code in contextimport { Address, getRandomB256 } from 'fuels';
const randomB256: string = getRandomB256();
const address = Address.fromB256(randomB256);
expect(address.toB256()).toEqual(randomB256);