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

Market

Binance.US Faces Regulatory Challenges in the United States

Regulators from both Florida and Alaska have requested Binance.US to cease its operations and refrain from providing ...

Bitcoin

RFK Jr. and Trump Share Similar Stance on US CBDC

Robert F. Kennedy Jr., a candidate who has shown progressive thinking by accepting BTC for his campaign, has made a b...

Blockchain

Blobs Launch on Gnosis Chain ahead of Ethereum's London Upgrade.

Gnosis Chain is taking a significant step in enhancing its ecosystem by integrating blobs. This strategic move also l...

Market

ORDI BRC-20 Perpetual Contracts: A Game Changer in Crypto Futures Trading

Exciting News Binance will now be offering the ORDI/USDC trading pair on its futures platform beginning February 22! ...

Market

The Bitcoin Party: Euphoria and Rate Cuts

Cryptocurrency analysts predict a Santa Claus rally in the market, anticipating Bitcoin to potentially reach $48K as ...

Web3

Web3 Investment Sees Major Capital Influx

Great news! GBA Capital Fund is proud to introduce a massive $10 billion investment fund specifically targeted toward...