|
1 | | -# Converting native types |
| 1 | +# Converting Types |
| 2 | + |
| 3 | +Below you can find examples for common type conversions: |
| 4 | + |
| 5 | +- [Convert Between Native Types](#convert-between-native-types) |
| 6 | +- [Convert to `Bytes32`](#convert-to-bytes32) |
| 7 | +- [Convert to `Address`](#convert-to-address) |
| 8 | +- [Convert to `ContractId`](#convert-to-contractid) |
| 9 | +- [Convert to `Identity`](#convert-to-identity) |
| 10 | +- [Convert to `AssetId`](#convert-to-assetid) |
| 11 | +- [Convert to `Bech32`](#convert-to-bech32) |
| 12 | +- [Convert to `str`](#convert-to-str) |
| 13 | +- [Convert to `Bits256`](#convert-to-bits256) |
| 14 | +- [Convert to `Bytes`](#convert-to-bytes) |
| 15 | +- [Convert to `B512`](#convert-to-b512) |
| 16 | +- [Convert to `EvmAddress`](#convert-to-evmaddress) |
| 17 | + |
| 18 | +## Convert Between Native Types |
2 | 19 |
|
3 | 20 | 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: |
4 | 21 |
|
5 | 22 | ```rust,ignore |
6 | 23 | {{#include ../../../examples/types/src/lib.rs:type_conversion}} |
7 | 24 | ``` |
| 25 | + |
| 26 | +## Convert to `Bytes32` |
| 27 | + |
| 28 | +Convert a `[u8; 32]` array to `Bytes32`: |
| 29 | + |
| 30 | +```rust,ignore |
| 31 | +{{#include ../../../examples/types/src/lib.rs:array_to_bytes32}} |
| 32 | +``` |
| 33 | + |
| 34 | +Convert a hex string to `Bytes32`: |
| 35 | + |
| 36 | +```rust,ignore |
| 37 | +{{#include ../../../examples/types/src/lib.rs:hex_string_to_bytes32}} |
| 38 | +``` |
| 39 | + |
| 40 | +## Convert to `Address` |
| 41 | + |
| 42 | +Convert a `[u8; 32]` array to an `Address`: |
| 43 | + |
| 44 | +```rust,ignore |
| 45 | +{{#include ../../../examples/types/src/lib.rs:array_to_address}} |
| 46 | +``` |
| 47 | + |
| 48 | +Convert a `Bech32` address to an `Address`: |
| 49 | + |
| 50 | +```rust,ignore |
| 51 | +{{#include ../../../examples/types/src/lib.rs:bech32_to_address}} |
| 52 | +``` |
| 53 | + |
| 54 | +Convert a wallet to an `Address`: |
| 55 | + |
| 56 | +```rust,ignore |
| 57 | +{{#include ../../../examples/wallets/src/lib.rs:wallet_to_address}} |
| 58 | +``` |
| 59 | + |
| 60 | +Convert a hex string to an `Address`: |
| 61 | + |
| 62 | +```rust,ignore |
| 63 | +{{#include ../../../examples/types/src/lib.rs:hex_string_to_address}} |
| 64 | +``` |
| 65 | + |
| 66 | +## Convert to `ContractId` |
| 67 | + |
| 68 | +Convert a `[u8; 32]` array to to `ContractId`: |
| 69 | + |
| 70 | +```rust,ignore |
| 71 | +{{#include ../../../examples/types/src/lib.rs:array_to_contract_id}} |
| 72 | +``` |
| 73 | + |
| 74 | +Convert a hex string to a `ContractId`: |
| 75 | + |
| 76 | +```rust,ignore |
| 77 | +{{#include ../../../examples/types/src/lib.rs:string_to_contract_id}} |
| 78 | +``` |
| 79 | + |
| 80 | +Convert a contract instance to a `ContractId`: |
| 81 | + |
| 82 | +```rust,ignore |
| 83 | +{{#include ../../../packages/fuels/tests/logs.rs:instance_to_contract_id}} |
| 84 | +``` |
| 85 | + |
| 86 | +## Convert to `Identity` |
| 87 | + |
| 88 | +Convert an `Address` to an `Identity`: |
| 89 | + |
| 90 | +```rust,ignore |
| 91 | +{{#include ../../../examples/types/src/lib.rs:address_to_identity}} |
| 92 | +``` |
| 93 | + |
| 94 | +Convert a `ContractId` to an `Identity`: |
| 95 | + |
| 96 | +```rust,ignore |
| 97 | +{{#include ../../../examples/types/src/lib.rs:contract_id_to_identity}} |
| 98 | +``` |
| 99 | + |
| 100 | +## Convert to `AssetId` |
| 101 | + |
| 102 | +Convert a `[u8; 32]` array to an `AssetId`: |
| 103 | + |
| 104 | +```rust,ignore |
| 105 | +{{#include ../../../examples/types/src/lib.rs:array_to_asset_id}} |
| 106 | +``` |
| 107 | + |
| 108 | +Convert a hex string to an `AssetId`: |
| 109 | + |
| 110 | +```rust,ignore |
| 111 | +{{#include ../../../examples/types/src/lib.rs:string_to_asset_id}} |
| 112 | +``` |
| 113 | + |
| 114 | +## Convert to `Bech32` |
| 115 | + |
| 116 | +Convert a `[u8; 32]` array to a `Bech32` address: |
| 117 | + |
| 118 | +```rust,ignore |
| 119 | +{{#include ../../../examples/types/src/lib.rs:array_to_bech32}} |
| 120 | +``` |
| 121 | + |
| 122 | +Convert `Bytes32` to a `Bech32` address: |
| 123 | + |
| 124 | +```rust,ignore |
| 125 | +{{#include ../../../examples/types/src/lib.rs:bytes32_to_bech32}} |
| 126 | +``` |
| 127 | + |
| 128 | +Convert a string to a `Bech32` address: |
| 129 | + |
| 130 | +```rust,ignore |
| 131 | +{{#include ../../../examples/types/src/lib.rs:str_to_bech32}} |
| 132 | +``` |
| 133 | + |
| 134 | +Convert an `Address` to a `Bech32` address: |
| 135 | + |
| 136 | +```rust,ignore |
| 137 | +{{#include ../../../examples/types/src/lib.rs:address_to_bech32}} |
| 138 | +``` |
| 139 | + |
| 140 | +## Convert to `str` |
| 141 | + |
| 142 | +Convert a `ContractId` to a `str`: |
| 143 | + |
| 144 | +```rust,ignore |
| 145 | +{{#include ../../../examples/types/src/lib.rs:contract_id_to_str}} |
| 146 | +``` |
| 147 | + |
| 148 | +Convert an `Address` to a `str`: |
| 149 | + |
| 150 | +```rust,ignore |
| 151 | +{{#include ../../../examples/types/src/lib.rs:address_to_str}} |
| 152 | +``` |
| 153 | + |
| 154 | +Convert an `AssetId` to a `str`: |
| 155 | + |
| 156 | +```rust,ignore |
| 157 | +{{#include ../../../examples/types/src/lib.rs:asset_id_to_str}} |
| 158 | +``` |
| 159 | + |
| 160 | +Convert `Bytes32` to a `str`: |
| 161 | + |
| 162 | +```rust,ignore |
| 163 | +{{#include ../../../examples/types/src/lib.rs:bytes32_to_str}} |
| 164 | +``` |
| 165 | + |
| 166 | +## Convert to `Bits256` |
| 167 | + |
| 168 | +Convert a hex string to `Bits256`: |
| 169 | + |
| 170 | +```rust,ignore |
| 171 | +{{#include ../../../packages/fuels-core/src/types/core/bits.rs:hex_str_to_bits256}} |
| 172 | +``` |
| 173 | + |
| 174 | +Convert a `ContractId` to `Bits256`: |
| 175 | + |
| 176 | +```rust,ignore |
| 177 | +{{#include ../../../examples/types/src/lib.rs:contract_id_to_bits256}} |
| 178 | +``` |
| 179 | + |
| 180 | +Convert an `Address` to `Bits256`: |
| 181 | + |
| 182 | +```rust,ignore |
| 183 | +{{#include ../../../examples/types/src/lib.rs:address_to_bits256}} |
| 184 | +``` |
| 185 | + |
| 186 | +Convert an `AssetId` to `Bits256`: |
| 187 | + |
| 188 | +```rust,ignore |
| 189 | +{{#include ../../../examples/types/src/lib.rs:asset_id_to_bits256}} |
| 190 | +``` |
| 191 | + |
| 192 | +## Convert to `Bytes` |
| 193 | + |
| 194 | +Convert a string to `Bytes`: |
| 195 | + |
| 196 | +```rust,ignore |
| 197 | +{{#include ../../../packages/fuels-core/src/types/core/bytes.rs:hex_string_to_bytes32}} |
| 198 | +``` |
| 199 | + |
| 200 | +## Convert to `B512` |
| 201 | + |
| 202 | +Convert two hex strings to `B512`: |
| 203 | + |
| 204 | +```rust,ignore |
| 205 | +{{#include ../../../packages/fuels/tests/types_contracts.rs:b512_example}} |
| 206 | +``` |
| 207 | + |
| 208 | +## Convert to `EvmAddress` |
| 209 | + |
| 210 | +Convert a `Bits256` address to an `EvmAddress`: |
| 211 | + |
| 212 | +```rust,ignore |
| 213 | +{{#include ../../../examples/types/src/lib.rs:b256_to_evm_address}} |
| 214 | +``` |
0 commit comments