forked from pynchmeister/LTV-Blockchain-Course
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBytes.sol
More file actions
43 lines (31 loc) · 940 Bytes
/
Copy pathBytes.sol
File metadata and controls
43 lines (31 loc) · 940 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;
contract Bytes {
/*
bytes - dynamically-sized byte array
bytes1 to bytes32 - fixed size byte arrays
*/
// dynamic bytes below
bytes public data;
function storeData() public {
data = "hello"; // stored as bytes: 0x68656c6c6f
}
// fixed-size bytes below
bytes32 public hash;
function storeHash() public {
hash = keccak256(abi.encodePacked("data"));
}
// Example: Bytes vs string
string public name = "Harris";
bytes public nameInBytes = bytes(name);
// name = "Harris" (text form)
// nameInBytes = 0x486172726973
// convert string to bytes
bytes public b = bytes("Welcome");
bytes
// acesss bytes like an array
function getFIrstByte() public pure returns (bytes1) {
bytes memory b = "Hello";
return b[0]; // 0x48 (ASCII for 'H'
}
}