Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About
知乎專欄多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者”

14.4. 數據類型

14.4.1. 數值型

int/uint:變長的有符號或無符號整型。變數支持的步長以8遞增,支持從uint8到uint256,以及int8到int256。需要注意的是,uint和int預設代表的是uint256和int256。

有符號整型能夠表示負數的代價是其能夠存儲正數的範圍的縮小,因為其約一半的數值範圍要用來表示負數。如:uint8的存儲範圍為0 ~ 255,而int8的範圍為-127 ~ 127

支持的運算符:



比較:<=,<,==,!=,>=,>,返回值為bool類型。

位運算符:&,|,(^異或),(~非)。

數學運算:+,-,一元運算+,*,/,(%求余),(**次方),(<<左移),(>>右移)。

小數由"."組成,在他的左邊或右邊至少要包含一個數字。如"1.",".1","1.3"均是有效的小數。

14.4.1.1. 加 +,減 -,乘 *,除 / 運算演示

				
pragma solidity ^0.4.25;
//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;
  }
}				
				
				

14.4.1.2. 求余 % 運算演示

				
pragma solidity ^0.4.25;
//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;
  }
}				
				
				

14.4.1.3. 冪運算演示

				
pragma solidity ^0.4.25;
//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;
  }

}				
				
				

14.4.1.4. 與 &,| 或,非 ~,異或 ^ 演示

				
pragma solidity ^0.4.25;
//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
  }
}
				
				

14.4.1.5. 位移演示

				
pragma solidity ^0.4.25;
//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次方。

14.4.2. 字元串

string 字元串類型,字元串可以通過""或者''來定義字元串的值

			
pragma solidity ^0.4.25;
//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;
    }
}			
			
			

14.4.2.1. 獲取字元串長度

在 Solidity 中想獲得字元串長度必須轉成 bytes 類型然後使用 length 屬性獲得。bytes(string).length

				
pragma solidity ^0.4.25;
//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。

14.4.3. 布爾(Booleans)

bool: 可能的取值為常量值true和false。支持的運算符:

			
! 邏輯非

&& 邏輯與

|| 邏輯或

== 等於

!= 不等於

bool a = true;
bool b = !a;

// a == b -> false
// a != b -> true
// a || b -> true
// a && b -> false			
			
			

14.4.4. 位元組類型

			

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.25;
//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.25;
//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);
    }
    
}			
			
			

14.4.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.25;
//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;
    }
    
}
			
			

14.4.5.1. length

.length 屬性是活動數組的尺寸

				
pragma solidity ^0.4.25;
//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;
    }
    
}				
				
				

14.4.5.2. push() 方法

通過 push 可以向數組中添加數據

				
pragma solidity ^0.4.25;
//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;
    }
    
}				
				
				

14.4.6. 枚舉類型

State 就是一個自定義的整型,當枚舉數不夠多時,它預設的類型為uint8,當枚舉數足夠多時,它會自動變成uint16,枚舉下標定義從左至右從零開始。

New=0, Pending=1, Done=2, Deleted=3

訪問枚舉方式 State.New 實際等於數字 0

			
pragma solidity ^0.4.25;
//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
}				
				
			

14.4.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.25;
//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;
    }
}				
				
			

14.4.7.1. 函數返回Struct

Struct 不知直接返回,解決方法如下

				
pragma solidity ^0.4.19;
//Author: netkiller <netkiller@msn.com>
//Home: http://www.netkiller.cn
contract Netkiller {
    struct JobStruct {
      uint a;
      uint b;
      uint c;
    }

    function getValues () public pure returns (uint, uint, uint) {
      JobStruct memory js = JobStruct(1, 2, 3);
      return (js.a, js.b, js.c);
    }
}				
				
				

14.4.8. address

				
address public minter;
				
			

下面是一個獲得賬號餘額的例子。

			
pragma solidity ^0.4.25;
//Author: netkiller <netkiller@msn.com>
//Home: http://www.netkiller.cn
contract AddressTest{
    
    function getBalance(address _addr) public constant returns (uint){
        return _addr.balance;
    }

}			
			
			

14.4.8.1. payable

14.4.8.2. .value()

14.4.8.3. .gas()

14.4.9. mapping

mapping 就是圖資料結構,由 key 和 value 組成。

			
pragma solidity ^0.4.25;
//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];
    }
}