Introduction to Technology | Solidity Programming Language: Byte Array, Mapping
Byte array
A byte array can be thought of as a special array whose element type is a byte. It has its own proprietary name when it comes to type. As an array he has an array of unfixed length bytes and a fixed length byte. 1. A fixed length byte array.
pragma solidity >=0.4.0 <0.6.0; contract EgFixedByteArray { byte[5] ba5; bytes5 bs5; function modify() public{ ba5 = [byte('1'),'2','3','4','5']; //ba5.push(6); //ba5.length = 6; //bs5 = [byte('1'),'2','3','4','5']; ba5[1] = '0'; bs5 = '12345'; //bs5.length = 6; //bs5.push(6); //bs5[1] = '0'; } }
pragma solidity >=0.4.0 <0.6.0; contract EgFixedByteArray { byte[5] ba5; bytes5 bs5; function modify() public{ ba5 = [byte('1'),'2','3','4','5']; //ba5.push(6); //ba5.length = 6; //bs5 = [byte('1'),'2','3','4','5']; ba5[1] = '0'; bs5 = '12345'; //bs5.length = 6; //bs5.push(6); //bs5[1] = '0'; } }
pragma solidity >=0.4.0 <0.6.0; contract EgFixedByteArray { byte[5] ba5; bytes5 bs5; function modify() public{ ba5 = [byte('1'),'2','3','4','5']; //ba5.push(6); //ba5.length = 6; //bs5 = [byte('1'),'2','3','4','5']; ba5[1] = '0'; bs5 = '12345'; //bs5.length = 6; //bs5.push(6); //bs5[1] = '0'; } }
1.1 Fixed-length byte arrays can use the following two methods: one is to use array names, and the other is to use keyword names. The number range after bytes is from 1 to 32, that is, bytes33 cannot be used. In actual coding, it is best to use this method if the length is determined. Byte[5] bs5; bytes5 bs5_; 1.2 No matter which way the name is, you can't modify length and use the push method. 1.3 bytes5 The sound name of this way can not be assigned using an array. 1.4 bytes5 The byte array of this name is unmodifiable. 2. Unfixed length byte array (dynamic array). Unfixed length byte arrays also have two ways of sound names, arrays and keywords. The characteristics of the array method is the characteristics of the array. Here we look at the way of using the keyword bytes.
- BTC search volume has climbed to a record high, and the mysterious netizen 4Chan's currency price forecast has to be realized?
- No loss of money or stolen money, this Thai mainstream exchange has announced the closure
- Burger King announced that it will accept bitcoin payments. Will you give up the currency and choose to buy a burger?
pragma solidity >=0.4.0 <0.6.0;
Contract EgUnFixedByteArray { Bytes name = new bytes(5);
Function modifyName() public returns( bytes memory) { Name.length = 6; Name.push('9'); Name[1] = '1'; Return name; }
}
3. Conversion Here we mainly talk about fixed-length byte arrays, unfixed length byte arrays, and conversions between strings. 3.1 Conversion between fixed-length byte arrays.
contract EgSwitchBytes{ function switchBytes1() public returns(bytes2) { bytes2 bs2 = '12'; bytes5 bs5 = '00000'; //return bytes2(bs5); return bytes2(bs5); } }
A fixed-length byte array with a longer length can be converted to a shorter one, and vice versa, and a long one that is shorter will intercept the length of the latter part. For example, if the upper bs5 is converted to bytes2, it will be '234. 'Truncate it. 3.2 fixed length byte array and non-fixed length byte array or string conversion function switchBytes2 () public { / * bytess5 bs5 = '00000'; bytes bs = bytes (bs5); / / fixed length byte array and not fixed Length byte arrays cannot be converted directly.
bytes memory bs = new bytes(10); bytes5 bs5 = bytes5(bs);//固定长度字节数组与不固定长度字节数组不能直接转换*/ bytes memory bs = new bytes(10); string memory str = string(bs);
String memory str1 = "abcd"; Bytes memory bs1 = bytes(str1);
/*bytes5 memory bs5 = '00000'; String memory str2 = string(bs5);//fixed-length byte array and string cannot be directly converted
String memory str3 = "abcd"; Bytes5 memory bs5_ = bytes5(str3); / / fixed length byte array and string can not be directly converted * / }
Fixed-length byte arrays with non-fixed-length byte arrays or strings (essentially non-fixed-length byte arrays) cannot be directly converted from each other, but strings and bytes can. 3.3 How to convert a fixed length byte array. Here is the idea, is to traverse a fixed-length byte array, and then assign the elements one by one to the non-fixed length byte array, so that you get an array of unfixed length bytes, you can also convert to an unfixed length byte array again String. One of the problems is that bytes5 bs5 = '1', the original bs5 type is bytes5, that is, it should contain 5 bytes, but the actual assignment is only '1', then the other 4 bytes are 啥? It is empty, that is, \u0000, which is a null character (not a space, nor a null), so four \u0000 will be traversed during the traversal. The result is that the unfixed length byte array contains four \u0000, and the converted string is also problematic. Consider the issue of \u0000 when converting. Bytes and strings are a special array. Bytes are similar to byte[], but when the external function is called as a parameter, it will be compressed and packed, which saves space, so you should try to use bytes4. String is similar to bytes, but does not provide length and serial access.
Mapping
The mapping feature of mapping is used to store key-value pairs. The writing method is somewhat different from the general programming language, mapping (keyType => valueType). Mapping can only be used in the state variable of the contract, or a reference to the storage within the function, such as the object of the var storage mappVal used to store the reference of the state variable, can not use the non-state variable to initialize the reference, that is, the mapping will eventually It is not possible to store memory variables in the blockchain.
pragma solidity >=0.4.0 <0.6.0;
Contract EgMapping{ Mapping(uint => string) public kvs;
Function put () public{ Kvs[1] = "a"; Kvs[2] = "b"; Mapping(uint => string) storage kvs1 = kvs; String memory a = kvs[1]; } }
1. The key of the mapping can use all types except the mapping type, and there is no limit to the value. 2. Mapping does not actually store the value of the key, but instead converts the key to a hash value of keccak256 for storage, so the saved key cannot be obtained by mapping. 3. Mapping can only be used to define state variables. If you want to use it inside a function, you need to name it a reference to a storage type. The reference points to a state variable. 4. Add elements such as kvs[1] = "a". 5. Update the element, just like the addition, except that the key already exists. 6. Find the element, such as string memory a = kvs[1]. 7. Delete the element and use the key delete, such as delete kvs[1]. Note that the delete operation modifies the state variable, so there will be consumption of gas, and it is generally not easy to delete the element. As mentioned above, mapping does not save the value of the key, so it is impossible to perform direct traversal of mapping. But if you save the key, you can traverse it. The specific implementation can be seen in this toolkit, which saves the key to an array of non-fixed length, so you need to use the insert method of the tool when inserting the element. Author: thank HPB Blue Lotus team finishing feeds. Wang Xiaoming blog: http://wangxiaoming.com/ Wang Xiaoming: founder of HPB core chain, Babbitt columnist. More than ten years of experience in financial big data and blockchain technology development, he has participated in the creation of UnionPay big data. The main creative blockchain teaching video program "Ming Shuo" more than 30 issues, compiled the "Ethernet official website document Chinese version", and as the main author wrote the "blockchain development guide", in the Chinese blockchain community with ID "blue lotus "famous.
We will continue to update Blocking; if you have any questions or suggestions, please contact us!
Was this article helpful?
93 out of 132 found this helpful
Related articles
- From Web2.0 to Web3.0: How far is it?
- Can the "leverage" of the currency security shake the BNB up again?
- Li Lihui, former president of Bank of China: Is digital currency another reconstruction of the global monetary system?
- Research | Blockchain thinking and intellectual property pledge financing industry
- ECB Executive Board member: Libra poses a threat to European monetary policy and weakens central bank control
- Bitcoin returned to $10,000 and the market share exceeded 70%. The analysis said that the parabolic rise has been turned on.
- Viewpoint | Blockchain game is not the future