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; } }

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; } }

  1. 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.
  2. 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!

Share:

Was this article helpful?

93 out of 132 found this helpful

Discover more

Blockchain

The money was not earned, and the head was almost bald: interview with the boss of the startup exchange

Currently, one of the most profitable industries in the cryptocurrency sector is the exchange. According to The Block...

Blockchain

Will Upbit's $ 50 million loss bring Defi's "prosperity"?

The South Korean exchange was stolen again. Following the theft of a South Korean exchange at the beginning of the ye...

Blockchain

Bitcoin for $0.32, they dreamed back to 2009 today

On August 23, 2019, the history of cryptocurrencies may remember this day. In the normal market price of Bitcoin was ...

Blockchain

New Battlefield for Encrypted Exchanges: High Frequency Trading

Some cryptocurrency exchanges are "quietly" paving the red carpet for High-Frequency Trading, and many trad...

Blockchain

OK Jumpstart and then the exchange "new hot" rules are too complicated for users to "do not understand"?

This afternoon, the digital asset exchange OKEx officially announced the sales rules of OK Jumpstart. The rule shows ...

Blockchain

Sun Yuchen used capital hegemony to control Steem, causing controversy, the integrity of stolen users' voting rights was questioned

Recently, in order to prevent capital power on the chain, Steem witness nodes jointly launched a soft fork. God V des...