Solidity - 赋值运算符



示例

尝试以下代码以在 Solidity 中实现赋值运算符。

pragma solidity ^0.5.0;

contract SolidityTest {
   uint storedData; 
   constructor() public{
      storedData = 10;   
   }
   function getResult() public view returns(string memory){
      uint a = 1; 
      uint b = 2;
      uint result = a + b;
      return integerToString(storedData); 
   }
   function integerToString(uint _i) internal pure 
      returns (string memory) {
      if (_i == 0) {
         return "0";
      }
      uint j = _i;
      uint len;
      while (j != 0) {
         len++;
         j /= 10; //assignment operation
      }
      bytes memory bstr = new bytes(len);
      uint k = len - 1;
      while (_i != 0) {
         bstr[k--] = byte(uint8(48 + _i % 10));
         _i /= 10;//assignment operation
      }
      return string(bstr);  //access local variable
   }
}

使用 Solidity 第一个应用程序 章节中提供的步骤运行以上程序。

输出

0: string: 10
solidity_operators.htm
广告