2017-11-06 22 views
0

Solidityでは、intを文字列に変換する方法はありますか?intを文字列に変換する方法はありますか?

例:

pragma solidity ^0.4.4; 

contract someContract { 

    uint i; 

    function test() pure returns (string) { 

     return "Here and Now is Happiness!"; 

    } 

    function love() pure returns(string) { 

     i = i +1; 

     return "I love " + functionname(i) + " persons" ; 
    } 



} 

functionnameとは何であるおかげで?!ここ

答えて

1

2つのポストは、回答を与えている:

https://ethereum.stackexchange.com/questions/10811/solidity-concatenate-uint-into-a-string

https://ethereum.stackexchange.com/questions/10932/how-to-convert-string-to-int

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); 
    } 

よろしく

+0

はい、それはOKです。おかげでTom。 –

+0

私はそれをテストしたときにバグだったので、Oraclizeのソリューションhttps://github.com/oraclize/ethereum-api/blob/master/oraclizeAPI_0.5.solが良いかもしれません: function uint2str(uint i)internal pure戻り値(文字列){ if(i == 0)は "0"を返します。 uint j = i; uintの長さ。 while(j!= 0){ length ++; length ++; j/= 10; } bytes memory bstr =新しいバイト(長さ)。 uint k =長さ - 1; while(i!= 0){ bstr [k--] = byte(48 + i%10); ; i = 10; } リターン文字列(bstr); } –

関連する問題