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

Bloomberg: The currency stability exchange's own stable currency will be issued in "weeks to one or two months"

According to Bloomberg News, Wei Zhou, chief financial officer of Binance, the main cryptocurrency exchange, said in ...

Blockchain

The head exchange spoiled, but who did not solve the Staking pain point?

It will seize more than 14% of the market share of the currency market, and the choice of the top 100 currencies of t...

Opinion

Wall Street Journal Binance Empire on the Verge of Collapse

After the collapse of FTX, the largest cryptocurrency exchange in the world seems to be Binance. However, less than a...

Blockchain

Is an exchange losing $ 250 million in cryptocurrencies a Ponzi scheme: Quadriga Bizarre Story

Written by: Nathaniel Rich Translator: Zhan Juan Illustrator: Bianca Bagnarelli Original article published in Vanity ...

Opinion

Data Perspective on the South Korean Cryptocurrency Market Strong Growth of CEX and Obsession of Retail Investors with Altcoins

We will study data from centralized exchanges in Korea and explore the characteristics and trends of Korean investors.

Blockchain

Is the 'big boss' of the cryptocurrency world, Binance, starting to decline?

LianGuaiBitpushNews Mary Liu As 2022 comes to a close, it seems that Zhao Changpeng, co-founder and CEO of Binance, ...