You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Type casting can be done both explicitly and implicitly as illustrated below. `pubkey`, `sig` and `datasig` can be implicitly cast to `bytes`, meaning they can be used anywhere where you would normally use a `bytes` type. Explicit type casting can be done with a broader range of types, but is still limited. The syntax of this explicit type casting is illustrated below. Note that you can also cast to bounded `bytes` types.
156
+
Type casting can be done both explicitly and implicitly depending on the type. `pubkey`, `sig` and `datasig` can be implicitly cast to `bytes`, meaning they can be used anywhere where you would normally use a `bytes` type. Explicit type casting can be done with a broader range of types, but is still limited. The syntax of this explicit type casting is illustrated below:
157
+
158
+
#### Example
159
+
```solidity
160
+
pubkey pk = pubkey(0x0000);
161
+
bytes editedPk = bytes(pk) + 0x1234;
162
+
bytes4 zeroBytes = bytes4(0); // 0x00000000
163
+
```
164
+
165
+
### Casting Table
157
166
158
167
See the following table for information on which types can be cast to other which other types.
159
168
@@ -167,44 +176,46 @@ See the following table for information on which types can be cast to other whic
167
176
| sig | bytes | bytes |
168
177
| datasig | bytes | bytes |
169
178
170
-
#### Example
171
-
```solidity
172
-
pubkey pk = pubkey(0x0000);
173
-
bytes editedPk = bytes(pk) + 0x1234;
174
-
bytes4 integer = bytes4(25);
175
-
```
176
-
177
179
### Int to Byte Casting
178
180
179
-
When casting integer types to bytes of a certain size, the integer value is padded with zeros, e.g. `bytes4(0) == 0x00000000`. It is also possible to pad with a variable number of zeros by passing in a `size` parameter, which indicates the size of the output, e.g. `bytes(0, 4 - 2) == 0x0000`. The size casting can be a very important feature when keeping local state in an nftCommitment or in the simulated state.
181
+
When casting integer types to bytes of a certain size, the integer value is padded with zeros, e.g. `bytes4(0) == 0x00000000`. It is also possible to pad with a variable number of zeros by passing in a `size` parameter, which indicates the size of the output, e.g. `bytes(0, 4 - 2) == 0x0000`.
180
182
181
183
:::tip
182
184
Using `bytes20 placeholderPkh= bytes20(0)` will generate a 20 byte zero-array programmatically, whereas
183
185
`bytes20 placeholderPkh= 0x0000000000000000000000000000000000000000` will actually take 20 bytes of space in your contract.
184
186
:::
185
187
186
-
:::note
188
+
Casting an integer to a fixed-size byte-length can be a very important when storing local state in an nftCommitment. Developers need to consider what the preferable fixed-size length is for each indivual case depending on the interger range. Below we add a table with information on the maximum integer size for satoshis and fungible CashTokens:
| CashTokens | 9223 quadrillion | 8 bytes for max supply token |
194
+
195
+
:::info
187
196
VM numbers follow Script Number format (A.K.A. CSCriptNum), to convert VM number to bytes or the reverse, it's recommended to use helper functions for these conversions from libraries like Libauth.
188
197
:::
189
198
190
-
### Semantic Byte Casting
199
+
### Bytes to BytesX Casting
191
200
192
-
When casting unbounded `bytes` types to bounded `bytes` types (such as `bytes20` or `bytes32`), this is a purely semantic cast. The bytes are not padded with zeros, and no checks are performed to ensure the cast bytes are of the correct length. This can be helpful in certain cases, such as `LockingBytecode`, which expects a specific length input.
201
+
If you do need to pad bytes to a specific length, you can convert the bytes to `int` first, and then cast to the bounded `bytes` type. This will pad the bytes with zeros to the specified length, like specified in the *Int to Byte Casting* section above.
If you do need to pad bytes to a specific length, you can convert the bytes to `int` first, and then cast to the bounded `bytes` type. This will pad the bytes with zeros to the specified length, like specified in the *Int to Byte Casting* section above.
210
+
### Semantic Byte Casting
211
+
212
+
When casting unbounded `bytes` types to bounded `bytes` types (such as `bytes20` or `bytes32`), this is a purely semantic cast. The bytes are not padded with zeros, and no checks are performed to ensure the cast bytes are of the correct length. This can be helpful in certain cases, such as `LockingBytecode`, which expects a specific length input.
0 commit comments