In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "what are the Solidity data types". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
4.5. Data type 4.5.1. Numerical type
Int/uint: a long signed or unsigned integer. The step size supported by the variable is increased by 8, from uint8 to uint256 and from int8 to int256. It is important to note that uint and int represent uint256 and int256 by default.
The price at which signed integers can represent negative numbers is the narrowing of the range in which they can store positive numbers, because about half of their numerical ranges are used to represent negative numbers. For example, the storage range of uint8 is 0 ~ 255, while that of int8 is-127 ~ 127.
Supported operators:
Comparison: the return value is of type bool.
Bit operator: &, |, (^ XOR), (~ non).
Mathematical operation: +, -, unary operation +, *, /, (% remainder), (* power), (move to the right).
The decimal is made up of "." The composition should contain at least one number on his left or right. Such as "1.", ".1" and "1.3" are all valid decimals.
4.5.1.1. Add +, subtract -, multiply *, divide / operate demo pragma solidity ^ 0.4.20 * / Home: http://www.netkiller.cncontract Math {function mul (int a, int b) public pure returns (int) {int c = a * b; return c;} function div (int a, int b) public pure returns (int) {int c = a / b; return c } function sub (int a, int b) public pure returns (int) {return a-b;} function add (int a, int b) public pure returns (int) {int c = a + b; return c;} 4.5.1.2. Find the remainder of% pragma solidity ^ 0.4.20. Author: netkiller / / Home: http://www.netkiller.cncontract Math {function m (int a, int b) public pure returns (int) {int c = a% b; return c;} 4.5.1.3. The exponentiation pragma solidity ^ 0.4.20 rampact author: netkiller / / Home: http://www.netkiller.cncontract Math {function m (uint a, uint b) public pure returns (uint) {uint c = a calendar; return c;} 4.5.1.4. And &, or, not ~, ^ XOR or pragma solidity ^ 0.4.20; author: netkiller / / Home: http://www.netkiller.cncontract Math {function yu () public pure returns (uint) {uint a = 3; / / 0b0011 uint b = 4; / / 0b0100 uint c = a & b; / / 0b0000 return c; / / 0} function huo () public pure returns (uint) {uint a = 3 / / 0b0011 uint b = 4; / / 0b0100 uint c = a | b; / / 0b0111 return c; / / 7} function fei () public pure returns (uint8) {uint8 a = 3; / / 0b00000011 uint8 c = ~ a; / / 0b11111100 return c; / 0} function yihuo () public pure returns (uint) {uint a = 3; / / 0b0011 uint b = 4 / / 0b0100 uint c = a ^ b; / / 0b0111 return c; / / 252}} 4.5.1.5. Displacement pragma solidity ^ 0.4.20 rampact author: netkiller / / Home: http://www.netkiller.cncontract Math {function leftShift () public pure returns (uint8) {uint8 a = 8; / / 0b00001000 uint8 c = a > 2; / / 0b00000010 return c; / / 2}}
A > n means that the binary bit of a moves n bits to the right, which is equivalent to a divided by 2 to the n power under the condition that there is no overflow.
4.5.2. String
String string type. A string can define the value of a string through "" or "'".
Pragma solidity ^ 0.4.20 netkiller / / Home: http://www.netkiller.cncontract StringTest {string name; function StringTest () public {name = "default";} function setName (string _ name) public {name = _ name;} function getName () public view returns (string) {return name;} 4.5.2.1. Get string length
If you want to get the string length in Solidity, you must convert it to type bytes and then use the length attribute to get it. Bytes (string). Length
Pragma solidity ^ 0.4.20 netkiller / / Home: http://www.netkiller.cncontract StringTest {string public name = "http://www.netkiller.cn"; function nameBytes () public constant returns (bytes) {return bytes (name);} function nameLength () public constant returns (uint) {return bytes (name). Length } function length (string _ name) public pure returns (uint) {return bytes (_ name) .length;}}
Prompt
Note: Chinese characters are encoded in UTF8. One Chinese character is equal to 3 bytes. When you use length ("King") test, the length will be 6.
4.5.3. Bull (Booleans)
Bool: the possible values are constant values true and false. Supported operators:
! Logic is not & & Logic and | | Logic or = = equal to! = is not equal to bool a = true;bool b =! false// a / a = = b-> false// a! = b-> true// a | | b-> true// a & & b-> false4.5.4. Byte type bytes names = "netkiller" bytes9 _ names = "netkiller"; bytes (name) [0] = 0xFwitterbytes memory _ tmp = new bytes (3); _ tmp [0] = 0x4eash accountmp [1] = 0x6fcletmp [2] = 0x6f clearma solidity ^ 0.4.20 acronym author: netkiller / / Home: http://www.netkiller.cncontract BytesTest {bytes names = "netkiller"; function get () public view returns (bytes) {return names } function getBytes2 () public pure returns (bytes2) {bytes9 _ names = "netkiller"; return bytes2 (_ names);} function bytesToString () public constant returns (string) {return string (names);} function copyBytes (bytes b) public pure returns (bytes) {bytes memory tmp = new bytes (b.length); for (uint I = 0; I)
< b.length; i++) { tmp[i] = b[i]; } return tmp; } function bytesToString2() public pure returns (string) { bytes memory _tmp = new bytes(3); _tmp[0] = 0x4e; _tmp[1] = 0x65; _tmp[2] = 0x6f; return string(_tmp); } } .length可以动态修改字节数组的长度 pragma solidity ^0.4.20;//Author: netkiller //Home: http://www.netkiller.cncontract BytesTest2 { // 初始化一个两个字节空间的字节数组 bytes public array = new bytes(2); // 设置修改字节数组的长度 function setLength(uint _len) public { array.length = _len; } // 返回字节数组的长度 function getLength() constant public returns (uint) { return array.length; } // 往字节数组中添加字节 function pushArray(byte _tmp) public{ array.push(_tmp); } }4.5.5. 数组//创建一个memory的数组 uint[] memory a = new uint[](7); uint[] x = [uint(1), 3, 4]; bytes memory b = new bytes(10); 二维数组 uint [2][3] T = [[1,2],[3,4],[5,6]];pragma solidity ^0.4.20;//Author: netkiller //Home: http://www.netkiller.cncontract ArrayTest { uint [] array = [1,2,3,4,5]; // 通过for循环计算数组内部的值的总和 function sum() constant public returns (uint) { uint num = 0; for(uint i = 0; i < array.length; i++) { num = num + array[i]; } return num; } function sumNumbers(uint[] _numbers) public pure returns (uint) { uint num = 0; for(uint i = 0; i < _numbers.length; i++) { num = num + _numbers[i]; } return num; } }4.5.5.1. length .length 属性是活动数组的尺寸 pragma solidity ^0.4.20;//Author: netkiller //Home: http://www.netkiller.cncontract ArrayLength { uint [] array = [1,2,3,4,5]; function getLength() public constant returns (uint) { return array.length; } }4.5.5.2. push() 方法 通过 push 可以向数组中添加数据 pragma solidity ^0.4.20;//Author: netkiller //Home: http://www.netkiller.cncontract ArrayLength { uint [] array = [1,2,3,4,5]; function pushArray() public { array.push(6); } function getLength() public constant returns (uint) { return array.length; } }4.5.6. 枚举类型 State 就是一个自定义的整型,当枚举数不够多时,它默认的类型为uint8,当枚举数足够多时,它会自动变成uint16,枚举下标定义从左至右从零开始。 New=0, Pending=1, Done=2, Deleted=3 访问枚举方式 State.New 实际等于数字 0 pragma solidity ^0.4.20;//Author: netkiller //Home: http://www.netkiller.cncontract EnumTest { enum State { New, Pending, Done, Deleted } State state = State.New; function set(State _state) public { state = _state; } function get() constant public returns (State) { return state; }} 枚举用来定义状态 pragma solidity ^0.4.0;contract Purchase { enum State { Created, Locked, Inactive } // Enum}4.5.7. 结构体 定义结构体 struct Voter { uint weight; // weight is accumulated by delegation bool voted; // if true, that person already voted address delegate; // person delegated to uint vote; // index of the voted proposal } // This is a type for a single proposal. struct Proposal { bytes32 name; // short name (up to 32 bytes) uint voteCount; // number of accumulated votes } 演示例子 pragma solidity ^0.4.20;//Author: netkiller //Home: http://www.netkiller.cncontract Students { struct Person { string name; uint age; uint class; } Person person = Person("Neo",18,1); function getPerson() public view returns(string){ return person.name; }}4.5.8. addressaddress public minter; 下面是一个获得账号余额的例子。 pragma solidity ^0.4.20;//Author: netkiller //Home: http://www.netkiller.cncontract AddressTest{ function getBalance(address _addr) public constant returns (uint){ return _addr.balance; }}4.5.8.1. payable4.5.8.2. .value()4.5.8.3. .gas()4.5.9. mapping mapping 就是图数据结构,由 key 和 value 组成。 pragma solidity ^0.4.20;//Author: netkiller //Home: http://www.netkiller.cncontract MappingExample { mapping(uint =>String) map; function put (uint key, string value) public {map [key] = value;} function get (uint key) constant public returns (string) {return map [key];}} "what are the Solidity data types"? thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.