以下是常见类型转换的示例:
Bytes32
Address
ContractId
Identity
AssetId
Bech32
str
Bits256
Bytes
B512
EvmAddress
你可能需要在原生类型之间进行转换(Bytes32
、Address
、ContractId
和 AssetId
)。因为这些类型都是 [u8; 32]
的包装器,所以转换只是简单地对其中一个进行解引用,并使用解引用后的值实例化另一个。以下是一个示例:
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);
Bytes32
将 [u8; 32]
数组转换为 Bytes32
:
let my_slice = [1u8; 32];
let b256 = Bytes32::new(my_slice);
将十六进制字符串转换为 Bytes32
:
let hex_str = "0x0000000000000000000000000000000000000000000000000000000000000000";
let b256 = Bytes32::from_str(hex_str)?;
Address
将 [u8; 32]
数组转换为 Address
:
let my_slice = [1u8; 32];
let address = Address::new(my_slice);
将 Bech32
地址转换为 Address
:
let _plain_address: Address = bech32_address.into();
将钱包转换为 Address
:
let wallet_unlocked = WalletUnlocked::new_random(None);
let address: Address = wallet_unlocked.address().into();
将十六进制字符串转换为 Address
:
let hex_str = "0x0000000000000000000000000000000000000000000000000000000000000000";
let address = Address::from_str(hex_str)?;
ContractId
将 [u8; 32]
数组转换为 ContractId
:
let my_slice = [1u8; 32];
let contract_id = ContractId::new(my_slice);
将十六进制字符串转换为 ContractId
:
let hex_str = "0x0000000000000000000000000000000000000000000000000000000000000000";
let contract_id = ContractId::from_str(hex_str)?;
将合约实例转换为 ContractId
:
let contract_id: ContractId = contract_instance.id().into();
Identity
将 Address
转换为 Identity
:
let _identity_from_address = Identity::Address(address);
将 ContractId
转换为 Identity
:
let _identity_from_contract_id = Identity::ContractId(contract_id);
AssetId
将 [u8; 32]
数组转换为 AssetId
:
let my_slice = [1u8; 32];
let asset_id = AssetId::new(my_slice);
将十六进制字符串转换为 AssetId
:
let hex_str = "0x0000000000000000000000000000000000000000000000000000000000000000";
let asset_id = AssetId::from_str(hex_str)?;
Bech32
将 [u8; 32]
数组转换为 Bech32
地址:
let hrp = "fuel";
let my_slice = [1u8; 32];
let _bech32_address = Bech32Address::new(hrp, my_slice);
将 Bytes32
转换为 Bech32
地址:
let my_hash = Bytes32::new([1u8; 32]);
let _bech32_address = Bech32Address::new(hrp, my_hash);
将字符串转换为 Bech32
地址:
let address = "fuel1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqsx2mt2";
let bech32_address = Bech32Address::from_str(address)?;
将 Address
转换为 Bech32
地址:
let plain_address = Address::new([0u8; 32]);
let bech32_address = Bech32Address::from(plain_address);
str
将 ContractId
转换为 str
:
let _str_from_contract_id: &str = contract_id.to_string().as_str();
将 Address
转换为 str
:
let _str_from_address: &str = address.to_string().as_str();
将 AssetId
转换为 str
:
let _str_from_asset_id: &str = asset_id.to_string().as_str();
将 Bytes32
转换为 str
:
let _str_from_bytes32: &str = b256.to_string().as_str();
Bits256
将十六进制字符串转换为 Bits256
:
let hex_str = "0x0101010101010101010101010101010101010101010101010101010101010101";
let bits256 = Bits256::from_hex_str(hex_str)?;
将 ContractId
转换为 Bits256
:
let _contract_id_to_bits_256 = Bits256(contract_id.into());
将 Address
转换为 Bits256
:
let bits_256 = Bits256(address.into());
将 AssetId
转换为 Bits256
:
let _asset_id_to_bits_256 = Bits256(asset_id.into());
Bytes
将字符串转换为 Bytes
:
let hex_str = "0x0101010101010101010101010101010101010101010101010101010101010101";
let bytes = Bytes::from_hex_str(hex_str)?;
B512
将两个十六进制字符串转换为 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));
EvmAddress
将 Bits256
地址转换为 EvmAddress
:
let _evm_address = EvmAddress::from(bits_256);