-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp10_byte_arrays.sol
37 lines (30 loc) · 1.06 KB
/
p10_byte_arrays.sol
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
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
contract Local {
// 1 byte = 8 bits
// 1 hexadecimal digit = 4 bits
// Everything that will be stored in the byte array will be in the form of hexadecimal digits.
// Fixed-size Byte Array
bytes3 public b3; // 3 bytes array // 0x000000
bytes2 public b2; // 2 bytes array // 0x0000
function setter() public {
b3='abc'; // 0x616263
b2='ab'; // 0x6162
// b2='a'; // 0x6100 padding of 0 added to if space is not occupied
// b3[0] = 'd'; // bytes array can't modify / immutable
}
// Dynamic-Size byte array
// bytes which is shorthand for bytes[]
bytes public b1="abc"; // 0x616263
function pushElement() public {
// b1.push('de'); // not correct
b1.push('d'); // 0x61626364
b1.push('e'); // 0x6162636465
}
function getElement(uint i) public view returns(bytes1) {
return b1[i];
}
function getLength() public view returns(uint) {
return b1.length;
}
}