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

Bitcoin

The Ripple case: Over or Underdog Victory?

Missed the latest in crypto this weekend? Catch up on the top stories here!

Opinion

Why is selling risk the good business model?

The top companies, the market makers, are the ones who sell risk. They are the giants who have stacked up their finan...

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...

Blockchain

From Caroline Ellison to Gary Wang The Fate of Bankman-Fried Hangs by a Thread, According to Bloomberg

Three ex-close friends and colleagues of FTX co-founder Sam Bankman-Fried have testified against him, possibly result...

Blockchain

After carrying a huge debt and shutting down TradeBlock, the former crypto empire DCG is now struggling for survival with one arm.

As the liquidity crisis in encryption erupted, the market declined, and the previous blind expansion and investment h...

Blockchain

Fake foreign exchange platform to enter the currency circle: reverse shouting, tampering with data, investors become the biggest victims

After the spread of money and funds, there has been a new routine in the currency circle – a false exchange. Pu...