您好,登錄后才能下訂單哦!
本篇內容介紹了“Solidity數據類型有哪些”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
int/uint:變長的有符號或無符號整型。變量支持的步長以8遞增,支持從uint8到uint256,以及int8到int256。需要注意的是,uint和int默認代表的是uint256和int256。
有符號整型能夠表示負數的代價是其能夠存儲正數的范圍的縮小,因為其約一半的數值范圍要用來表示負數。如:uint8的存儲范圍為0 ~ 255,而int8的范圍為-127 ~ 127
支持的運算符:
比較:<=,<,==,!=,>=,>,返回值為bool類型。
位運算符:&,|,(^異或),(~非)。
數學運算:+,-,一元運算+,*,/,(%求余),(**次方),(<<左移),(>>右移)。
小數由"."組成,在他的左邊或右邊至少要包含一個數字。如"1.",".1","1.3"均是有效的小數。
pragma solidity ^0.4.20; //Author: netkiller <netkiller@msn.com> //Home: http://www.netkiller.cn contract 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; } }
pragma solidity ^0.4.20; //Author: netkiller <netkiller@msn.com> //Home: http://www.netkiller.cn contract Math { function m(int a, int b) public pure returns (int) { int c = a % b; return c; } }
pragma solidity ^0.4.20; //Author: netkiller <netkiller@msn.com> //Home: http://www.netkiller.cn contract Math { function m(uint a, uint b) public pure returns (uint) { uint c = a**b; return c; } }
pragma solidity ^0.4.20; //Author: netkiller <netkiller@msn.com> //Home: http://www.netkiller.cn contract 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 } }
pragma solidity ^0.4.20; //Author: netkiller <netkiller@msn.com> //Home: http://www.netkiller.cn contract Math { function leftShift() public pure returns (uint8) { uint8 a = 8; // 0b00001000 uint8 c = a << 2; // 0b00100000 return c; // 32 } function rightShift() public pure returns (uint8) { uint8 a = 8; // 0b00001000 uint8 c = a >> 2; // 0b00000010 return c; // 2 } }
a << n 表示a的二進制位向左移動n位,在保證位數沒有溢出的情況下等價于 a乘以2的n次方。
a >> n 表示a的二進制位向右移動n位,在保證位數沒有溢出的情況下等價于 a除以2的n次方。
string 字符串類型,字符串可以通過""或者''來定義字符串的值
pragma solidity ^0.4.20; //Author: netkiller <netkiller@msn.com> //Home: http://www.netkiller.cn contract StringTest { string name; function StringTest() public{ name = "default"; } function setName(string _name) public{ name = _name; } function getName() public view returns(string){ return name; } }
在 Solidity 中想獲得字符串長度必須轉成 bytes 類型然后使用 length 屬性獲得。bytes(string).length
pragma solidity ^0.4.20; //Author: netkiller <netkiller@msn.com> //Home: http://www.netkiller.cn contract 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; } }
提示 | |
---|---|
注意:漢字采用UTF8編碼,一個漢字等于3個字節,當你使用 length("景峯") 測試時會返回長度 6。 |
bool: 可能的取值為常量值true和false。支持的運算符:
! 邏輯非 && 邏輯與 || 邏輯或 == 等于 != 不等于 bool a = true; bool b = !a; // a == b -> false // a != b -> true // a || b -> true // a && b -> false
bytes names = "netkiller" bytes9 _names = "netkiller"; bytes(name)[0] = 0xFF; bytes memory _tmp = new bytes(3); _tmp[0] = 0x4e; _tmp[1] = 0x65; _tmp[2] = 0x6f;
pragma solidity ^0.4.20; //Author: netkiller <netkiller@msn.com> //Home: http://www.netkiller.cn contract 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 <netkiller@msn.com> //Home: http://www.netkiller.cn contract 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); } }
//創建一個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 <netkiller@msn.com> //Home: http://www.netkiller.cn contract 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; } }
.length 屬性是活動數組的尺寸
pragma solidity ^0.4.20; //Author: netkiller <netkiller@msn.com> //Home: http://www.netkiller.cn contract ArrayLength { uint [] array = [1,2,3,4,5]; function getLength() public constant returns (uint) { return array.length; } }
通過 push 可以向數組中添加數據
pragma solidity ^0.4.20; //Author: netkiller <netkiller@msn.com> //Home: http://www.netkiller.cn contract ArrayLength { uint [] array = [1,2,3,4,5]; function pushArray() public { array.push(6); } function getLength() public constant returns (uint) { return array.length; } }
State 就是一個自定義的整型,當枚舉數不夠多時,它默認的類型為uint8,當枚舉數足夠多時,它會自動變成uint16,枚舉下標定義從左至右從零開始。
New=0, Pending=1, Done=2, Deleted=3
訪問枚舉方式 State.New 實際等于數字 0
pragma solidity ^0.4.20; //Author: netkiller <netkiller@msn.com> //Home: http://www.netkiller.cn contract 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 }
定義結構體
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 <netkiller@msn.com> //Home: http://www.netkiller.cn contract Students { struct Person { string name; uint age; uint class; } Person person = Person("Neo",18,1); function getPerson() public view returns(string){ return person.name; } }
address public minter;
下面是一個獲得賬號余額的例子。
pragma solidity ^0.4.20; //Author: netkiller <netkiller@msn.com> //Home: http://www.netkiller.cn contract AddressTest{ function getBalance(address _addr) public constant returns (uint){ return _addr.balance; } }
mapping 就是圖數據結構,由 key 和 value 組成。
pragma solidity ^0.4.20; //Author: netkiller <netkiller@msn.com> //Home: http://www.netkiller.cn contract 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]; } }
“Solidity數據類型有哪些”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。