Solidity

如何將字元串轉換為int

  • July 21, 2020

如何將字元串轉換為int?這是我的程式碼:

  pragma solidity ^0.4.6;

contract MyContract {
   string public a;
   /* Constructor */
   function MyContract() {
       a = "0.12312317314571638713891378174163782169246891247193811231231731";
   }

   function bytesToUInt(uint v) constant returns (uint ret) {
   if (v == 0) {
       ret = 0;
   }
   else {
       while (v > 0) {
           ret = uint(uint(ret) / (2 ** 8));
           ret |= uint(((v % 10) + 48) * 2 ** (8 * 31));
           v /= 10;
       }
   }
   return ret;
}

   function get() constant returns(string){
       return a;
   }
}

請注意,目前 Solidity 和乙太坊虛擬機不支持浮點數。

當您的問題是關於將 a 轉換為stringint,您的範常式式碼指的是uint而不是int,因此我將提供將字元串轉換為 a 的解決方案uint

以下是將非浮點數從字元串轉換為 a uint(即 a uint256)的解決方案。我已將此問題的解決方案添加到我的Solidity 將 uint 連接成字元串的解決方案中?

pragma solidity ^0.4.4;

contract TestIntToString {

   string public uintToStringResult;
   string public appendUintToStringResult;
   uint public stringToUintResult;

   function TestIntToString() {
       uintToStringResult = uintToString(12345678901234567890);
       appendUintToStringResult = appendUintToString("My integer is: ", 1234567890);
       stringToUintResult = stringToUint("12312317314571638713891378174163782169246891247193811231231731");
   }

   function uintToString(uint v) constant returns (string str) {
       uint maxlength = 100;
       bytes memory reversed = new bytes(maxlength);
       uint i = 0;
       while (v != 0) {
           uint remainder = v % 10;
           v = v / 10;
           reversed[i++] = byte(48 + remainder);
       }
       bytes memory s = new bytes(i + 1);
       for (uint j = 0; j <= i; j++) {
           s[j] = reversed[i - j];
       }
       str = string(s);
   }

   function appendUintToString(string inStr, uint v) constant returns (string str) {
       uint maxlength = 100;
       bytes memory reversed = new bytes(maxlength);
       uint i = 0;
       while (v != 0) {
           uint remainder = v % 10;
           v = v / 10;
           reversed[i++] = byte(48 + remainder);
       }
       bytes memory inStrb = bytes(inStr);
       bytes memory s = new bytes(inStrb.length + i + 1);
       uint j;
       for (j = 0; j < inStrb.length; j++) {
           s[j] = inStrb[j];
       }
       for (j = 0; j <= i; j++) {
           s[j + inStrb.length] = reversed[i - j];
       }
       str = string(s);
   }

   function stringToUint(string s) constant returns (uint result) {
       bytes memory b = bytes(s);
       uint i;
       result = 0;
       for (i = 0; i < b.length; i++) {
           uint c = uint(b[i]);
           if (c >= 48 && c <= 57) {
               result = result * 10 + (c - 48);
           }
       }
   }
}

這是顯示解決方案有效的瀏覽器 Solidity 螢幕截圖:

在此處輸入圖像描述

BokkyPooBah 的一些程式碼不正確。

以下是將 uInt 轉換為字元串的函式,反之亦然,以及我的評論:

function stringToUint(string s) constant returns (uint) {
   bytes memory b = bytes(s);
   uint result = 0;
   for (uint i = 0; i < b.length; i++) { // c = b[i] was not needed
       if (b[i] >= 48 && b[i] <= 57) {
           result = result * 10 + (uint(b[i]) - 48); // bytes and int are not compatible with the operator -.
       }
   }
   return result; // this was missing
}

function uintToString(uint v) constant returns (string) {
   uint maxlength = 100;
   bytes memory reversed = new bytes(maxlength);
   uint i = 0;
   while (v != 0) {
       uint remainder = v % 10;
       v = v / 10;
       reversed[i++] = byte(48 + remainder);
   }
   bytes memory s = new bytes(i); // i + 1 is inefficient
   for (uint j = 0; j < i; j++) {
       s[j] = reversed[i - j - 1]; // to avoid the off-by-one error
   }
   string memory str = string(s);  // memory isn't implicitly convertible to storage
   return str;
}

引用自:https://ethereum.stackexchange.com/questions/10932