Icon HelpCircleForumIcon Link

⌘K

Icon HelpCircleForumIcon Link

Icon LinkConverting Types

Below you can find examples for common type conversions:

Icon LinkConvert Between Native Types

You might want to convert between the native types (Bytes32, Address, ContractId, and AssetId). Because these types are wrappers on [u8; 32], converting is a matter of dereferencing one and instantiating the other using the dereferenced value. Here's an example:

use fuels::types::{AssetId, ContractId};
 
let contract_id = ContractId::new([1u8; 32]);
 
let asset_id: AssetId = AssetId::new(*contract_id);
 
assert_eq!([1u8; 32], *asset_id);

Icon LinkConvert to Bytes32

Convert a [u8; 32] array to Bytes32:

let my_slice = [1u8; 32];
let b256 = Bytes32::new(my_slice);

Convert a hex string to Bytes32:

let hex_str = "0x0000000000000000000000000000000000000000000000000000000000000000";
let b256 = Bytes32::from_str(hex_str)?;

Icon LinkConvert to Address

Convert a [u8; 32] array to an Address:

let my_slice = [1u8; 32];
let address = Address::new(my_slice);

Convert a Bech32 address to an Address:

let _plain_address: Address = bech32_address.into();

Convert a wallet to an Address:

let wallet_unlocked = WalletUnlocked::new_random(None);
let address: Address = wallet_unlocked.address().into();

Convert a hex string to an Address:

let hex_str = "0x0000000000000000000000000000000000000000000000000000000000000000";
let address = Address::from_str(hex_str)?;

Icon LinkConvert to ContractId

Convert a [u8; 32] array to ContractId:

let my_slice = [1u8; 32];
let contract_id = ContractId::new(my_slice);

Convert a hex string to a ContractId:

let hex_str = "0x0000000000000000000000000000000000000000000000000000000000000000";
let contract_id = ContractId::from_str(hex_str)?;

Convert a contract instance to a ContractId:

let contract_id: ContractId = contract_instance.id().into();

Icon LinkConvert to Identity

Convert an Address to an Identity:

let _identity_from_address = Identity::Address(address);

Convert a ContractId to an Identity:

let _identity_from_contract_id = Identity::ContractId(contract_id);

Icon LinkConvert to AssetId

Convert a [u8; 32] array to an AssetId:

let my_slice = [1u8; 32];
let asset_id = AssetId::new(my_slice);

Convert a hex string to an AssetId:

let hex_str = "0x0000000000000000000000000000000000000000000000000000000000000000";
let asset_id = AssetId::from_str(hex_str)?;

Icon LinkConvert to Bech32

Convert a [u8; 32] array to a Bech32 address:

let hrp = "fuel";
let my_slice = [1u8; 32];
let _bech32_address = Bech32Address::new(hrp, my_slice);

Convert Bytes32 to a Bech32 address:

let my_hash = Bytes32::new([1u8; 32]);
let _bech32_address = Bech32Address::new(hrp, my_hash);

Convert a string to a Bech32 address:

let address = "fuel1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqsx2mt2";
let bech32_address = Bech32Address::from_str(address)?;

Convert an Address to a Bech32 address:

let plain_address = Address::new([0u8; 32]);
let bech32_address = Bech32Address::from(plain_address);

Icon LinkConvert to str

Convert a ContractId to a str:

let _str_from_contract_id: &str = contract_id.to_string().as_str();

Convert an Address to a str:

let _str_from_address: &str = address.to_string().as_str();

Convert an AssetId to a str:

let _str_from_asset_id: &str = asset_id.to_string().as_str();

Convert Bytes32 to a str:

let _str_from_bytes32: &str = b256.to_string().as_str();

Icon LinkConvert to Bits256

Convert a hex string to Bits256:

let hex_str = "0x0101010101010101010101010101010101010101010101010101010101010101";
 
let bits256 = Bits256::from_hex_str(hex_str)?;

Convert a ContractId to Bits256:

let _contract_id_to_bits_256 = Bits256(contract_id.into());

Convert an Address to Bits256:

let bits_256 = Bits256(address.into());

Convert an AssetId to Bits256:

let _asset_id_to_bits_256 = Bits256(asset_id.into());

Icon LinkConvert to Bytes

Convert a string to Bytes:

let hex_str = "0x0101010101010101010101010101010101010101010101010101010101010101";
 
let bytes = Bytes::from_hex_str(hex_str)?;

Icon LinkConvert to B512

Convert two hex strings to B512:

let hi_bits = Bits256::from_hex_str(
    "0xbd0c9b8792876713afa8bff383eebf31c43437823ed761cc3600d0016de5110c",
)?;
let lo_bits = Bits256::from_hex_str(
    "0x44ac566bd156b4fc71a4a4cb2655d3dd360c695edb17dc3b64d611e122fea23d",
)?;
let b512 = B512::from((hi_bits, lo_bits));

Icon LinkConvert to EvmAddress

Convert a Bits256 address to an EvmAddress:

let _evm_address = EvmAddress::from(bits_256);