Icon SunFilledIcon MoonStars

Icon LinkBytes32

In Sway and the FuelVM, bytes32 is used to represent hashes. It holds a 256-bit (32-bytes) value.

Icon LinkGenerating Random bytes32 Values

To generate a random bytes32 value, you can use the randomBytes function from the fuels module:

import { randomBytes } from 'fuels';
 
const bytes32: Bytes = randomBytes(32);

Icon LinkConverting Between Byte Arrays and Strings

You can use the hexlify function to convert a byte array to a hex string, and the arrayify function to convert a hex string back to a byte array:

import { randomBytes } from 'fuels';
 
const bytes32: Bytes = randomBytes(32);
 
const bytes32String: string = hexlify(bytes32);
 
// safely pass a 32-byte array into arrayify
expect(arrayify(bytes32)).toEqual(arrayify(bytes32String));
 
// a byte32 can be safely passed into hexlify more than once
expect(bytes32String).toEqual(hexlify(bytes32String));

Icon LinkWorking with b256 in Fuel

In Fuel, there is a special type called b256, which is similar to bytes32. Like bytes32, b256 is also used to represent hashes and holds a 256-bit value. You can learn more about working with b256 values in the Bits256 documentation .

Was this page helpful?