Solidity programming language: hex string, enum enum
Hexadecimal string
The hexadecimal string hex is not a Solidity data type because you cannot use hex as a type. When hex is added to the front of the literal, its role is to define the data format of the literal, which must be an even-numbered string of [0-9a-fA-F]. This way, when a specific data type is used to reference a hex string, it is implicitly converted. For example, string memory h = hex"010A31", the actual content of the converted string h is \u0001\n1.
In addition, when using a fixed-length byte array of types bytes4, the hex length cannot exceed the actual length of the reference type. For example, bytes4 b = hex "AABBccddee" cannot be compiled.
pragma solidity >=0.4.0 <0.6.0; contract EgHex{ function test() public returns(string memory){ string memory h = hex"010A31"; //string memory h = hex"010G" return h; } function test1() public returns(string memory){ string memory h = hex"010A"; return h; } function test2() public returns(bytes4){ //bytes4 b = hex"AABBccddee"; bytes4 c = hex"AABB"; bytes4 b = hex"AABBccdd"; return b; } function test3() public returns(bytes memory){ bytes memory b = hex"AABBccdd"; return b; } }
pragma solidity >=0.4.0 <0.6.0; contract EgHex{ function test() public returns(string memory){ string memory h = hex"010A31"; //string memory h = hex"010G" return h; } function test1() public returns(string memory){ string memory h = hex"010A"; return h; } function test2() public returns(bytes4){ //bytes4 b = hex"AABBccddee"; bytes4 c = hex"AABB"; bytes4 b = hex"AABBccdd"; return b; } function test3() public returns(bytes memory){ bytes memory b = hex"AABBccdd"; return b; } }
- The king of the public chain was surrounded by the chain, can the Ethereum return to the king?
- "Reverse refers to the king" Goldman Sachs milk was once again fulfilled, counting those "goos"
- Twitter Featured | Gold vs Bitcoin big debate, betting on the best assets in the next 10 years
pragma solidity >=0.4.0 <0.6.0; contract EgHex{ function test() public returns(string memory){ string memory h = hex"010A31"; //string memory h = hex"010G" return h; } function test1() public returns(string memory){ string memory h = hex"010A"; return h; } function test2() public returns(bytes4){ //bytes4 b = hex"AABBccddee"; bytes4 c = hex"AABB"; bytes4 b = hex"AABBccdd"; return b; } function test3() public returns(bytes memory){ bytes memory b = hex"AABBccdd"; return b; } }
pragma solidity >=0.4.0 <0.6.0; contract EgHex{ function test() public returns(string memory){ string memory h = hex"010A31"; //string memory h = hex"010G" return h; } function test1() public returns(string memory){ string memory h = hex"010A"; return h; } function test2() public returns(bytes4){ //bytes4 b = hex"AABBccddee"; bytes4 c = hex"AABB"; bytes4 b = hex"AABBccdd"; return b; } function test3() public returns(bytes memory){ bytes memory b = hex"AABBccdd"; return b; } }
pragma solidity >=0.4.0 <0.6.0; contract EgHex{ function test() public returns(string memory){ string memory h = hex"010A31"; //string memory h = hex"010G" return h; } function test1() public returns(string memory){ string memory h = hex"010A"; return h; } function test2() public returns(bytes4){ //bytes4 b = hex"AABBccddee"; bytes4 c = hex"AABB"; bytes4 b = hex"AABBccdd"; return b; } function test3() public returns(bytes memory){ bytes memory b = hex"AABBccdd"; return b; } }
Enumen enum
Enumeration types in Solidity are basically the same as in other programming languages. Let's look at an example, such as defining a season's enumeration.
Pragma solidity >=0.4.0 <0.6.0;
contract EgEnum{ enum Season{Spring, Summer, Autumn, Winter}
Function printSeason(Season s) public returns(Season) { return s; }
Function test1() public returns(Season){ return printSeason(Season.Spring); }
Function test2() public returns(uint){ uint s = uint(Season.Spring); return s; }
Function test3() public returns(Season){ //Season s = Season(5);//Boundary Season s = Season(3); return s; } }
- The actual type of enum is an unsigned integer. When the number of enumerations is in the range of 0-127, then enum is of type uint8. If it is in the range of 0-32,767, then enum is uint16 type, and so on.
- Since enum is a uint type, you can do type conversion. For example, uint s = uint(Season.Spring) converts the enumeration of the Season type to uint, and of course you can convert it to uint8, as long as it does not cross the boundary. Similarly, Season s = Season(3) converts uint to Season type. Also note that the integer does not exceed the range of the enumeration. For example, Season's range is 0-3. If you convert 5 to Season, you will run an exception and the compiler will pass.
Author: thank HPB technology community organize 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
- 2.8% bitcoin wallet address control 95% supply? The five reasons tell you that this conclusion is right.
- BTC fell below the $10,000 mark for the first time in half a month, with a negative correlation signal with gold prices
- US Internal Revenue Service upgrades, sending credits to cryptocurrency users
- The data shows that the positive correlation between Bitcoin and gold is gradually strengthening.
- HOLder – the wife of the blockchain industry
- Opinion | Bitcoin is enough, why do you need other coins?
- Going back to the Wandao mark again, but the market is still not optimistic.