[英]How to convert uint to string in solidity?
在 Solidity 中,有沒有辦法可以將我的 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" ;
}
}
什么是函數名?謝謝!
Solidity 0.8.0 更新:
來自https://github.com/provable-things/ethereum-api/blob/master/provableAPI_0.6.sol的uint2str()
函數現在已經過時,無法工作,但這里是更新的代碼,它使用了solidity 0.8.0 :(在上一版本中有一個溢出錯誤,但solidity <0.8.0 忽略了它,因為它不影響答案,但現在拋出錯誤) byte
也更改為bytes1
和+、-、* 和就像他們從 SafeMath 庫中那樣工作。
function uint2str(uint _i) internal pure returns (string memory _uintAsString) {
if (_i == 0) {
return "0";
}
uint j = _i;
uint len;
while (j != 0) {
len++;
j /= 10;
}
bytes memory bstr = new bytes(len);
uint k = len;
while (_i != 0) {
k = k-1;
uint8 temp = (48 + uint8(_i - _i / 10 * 10));
bytes1 b1 = bytes1(temp);
bstr[k] = b1;
_i /= 10;
}
return string(bstr);
}
這里的兩個帖子給出了回應:
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);
}
問候
在對已接受答案的評論中建議的可證明事物代碼對我"uintToStr": Avoid assigning to function parameters. [security/no-assign-params]
,但我的"uintToStr": Avoid assigning to function parameters. [security/no-assign-params]
發出了一個警告,即: "uintToStr": Avoid assigning to function parameters. [security/no-assign-params]
"uintToStr": Avoid assigning to function parameters. [security/no-assign-params]
。 下面稍微更改了原始代碼以更正此問題(將參數_i
重新分配給另一個名為number
變量):
/// @notice converts number to string
/// @dev source: https://github.com/provable-things/ethereum-api/blob/master/oraclizeAPI_0.5.sol#L1045
/// @param _i integer to convert
/// @return _uintAsString
function uintToStr(uint _i) internal pure returns (string memory _uintAsString) {
uint number = _i;
if (number == 0) {
return "0";
}
uint j = number;
uint len;
while (j != 0) {
len++;
j /= 10;
}
bytes memory bstr = new bytes(len);
uint k = len - 1;
while (number != 0) {
bstr[k--] = byte(uint8(48 + number % 10));
number /= 10;
}
return string(bstr);
}
Concrete_Buddhas 答案在 Solidity 0.8.0 中不起作用。 這是修訂版:
function uint2str(
uint256 _i
)
internal
pure
returns (string memory str)
{
if (_i == 0)
{
return "0";
}
uint256 j = _i;
uint256 length;
while (j != 0)
{
length++;
j /= 10;
}
bytes memory bstr = new bytes(length);
uint256 k = length;
j = _i;
while (j != 0)
{
bstr[--k] = bytes1(uint8(48 + j % 10));
j /= 10;
}
str = string(bstr);
}
如果您需要選擇性地轉換為科學記數法,例如為了更緊湊的數字表示,這里有一個用於此目的的修改版本:
function uintToString(uint v, bool scientific) public pure returns (string memory str) {
if (v == 0) {
return "0";
}
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(uint8(48 + remainder));
}
uint zeros = 0;
if (scientific) {
for (uint k = 0; k < i; k++) {
if (reversed[k] == '0') {
zeros++;
} else {
break;
}
}
}
uint len = i - (zeros > 2 ? zeros : 0);
bytes memory s = new bytes(len);
for (uint j = 0; j < len; j++) {
s[j] = reversed[i - j - 1];
}
str = string(s);
if (scientific && zeros > 2) {
str = string(abi.encodePacked(s, "e", uintToString(zeros, false)));
}
}
一些單元測試:
function testUintToString() public {
Assert.equal(Utils.uintToString(0, true), '0', '0');
Assert.equal(Utils.uintToString(1, true), '1', '1');
Assert.equal(Utils.uintToString(123, true), '123', '123');
Assert.equal(Utils.uintToString(107680546035, true), '107680546035', '107680546035');
Assert.equal(Utils.uintToString(1e9, true), '1e9', '1e9');
Assert.equal(Utils.uintToString(1 ether, true), '1e18', '1 ether');
Assert.equal(Utils.uintToString(550e8, true), '55e9', '55e9');
}
上面的代碼片段與solidity 0.6.0
兼容。
solidity ^0.8.0
import "@openzeppelin/contracts/utils/Strings.sol";
Strings.toString(myUINT)
為我工作。
雖然公認的答案似乎是正確的,但在大量數據上效率很低。 這是我的做法:
function itoa32 (uint x) private pure returns (uint y) {
unchecked {
require (x < 1e32);
y = 0x3030303030303030303030303030303030303030303030303030303030303030;
y += x % 10; x /= 10;
y += x % 10 << 8; x /= 10;
y += x % 10 << 16; x /= 10;
y += x % 10 << 24; x /= 10;
y += x % 10 << 32; x /= 10;
y += x % 10 << 40; x /= 10;
y += x % 10 << 48; x /= 10;
y += x % 10 << 56; x /= 10;
y += x % 10 << 64; x /= 10;
y += x % 10 << 72; x /= 10;
y += x % 10 << 80; x /= 10;
y += x % 10 << 88; x /= 10;
y += x % 10 << 96; x /= 10;
y += x % 10 << 104; x /= 10;
y += x % 10 << 112; x /= 10;
y += x % 10 << 120; x /= 10;
y += x % 10 << 128; x /= 10;
y += x % 10 << 136; x /= 10;
y += x % 10 << 144; x /= 10;
y += x % 10 << 152; x /= 10;
y += x % 10 << 160; x /= 10;
y += x % 10 << 168; x /= 10;
y += x % 10 << 176; x /= 10;
y += x % 10 << 184; x /= 10;
y += x % 10 << 192; x /= 10;
y += x % 10 << 200; x /= 10;
y += x % 10 << 208; x /= 10;
y += x % 10 << 216; x /= 10;
y += x % 10 << 224; x /= 10;
y += x % 10 << 232; x /= 10;
y += x % 10 << 240; x /= 10;
y += x % 10 << 248;
}
}
function itoa (uint x) internal pure returns (string memory s) {
unchecked {
if (x == 0) return "0";
else {
uint c1 = itoa32 (x % 1e32);
x /= 1e32;
if (x == 0) s = string (abi.encode (c1));
else {
uint c2 = itoa32 (x % 1e32);
x /= 1e32;
if (x == 0) {
s = string (abi.encode (c2, c1));
c1 = c2;
} else {
uint c3 = itoa32 (x);
s = string (abi.encode (c3, c2, c1));
c1 = c3;
}
}
uint z = 0;
if (c1 >> 128 == 0x30303030303030303030303030303030) { c1 <<= 128; z += 16; }
if (c1 >> 192 == 0x3030303030303030) { c1 <<= 64; z += 8; }
if (c1 >> 224 == 0x30303030) { c1 <<= 32; z += 4; }
if (c1 >> 240 == 0x3030) { c1 <<= 16; z += 2; }
if (c1 >> 248 == 0x30) { z += 1; }
assembly {
let l := mload (s)
s := add (s, z)
mstore (s, sub (l, z))
}
}
}
}
解釋
itoa32
function 將低於 10^32 的數字轉換為 32 位數字,必要時用零填充。
itoa
function 最多調用itoa32
3 次以轉換任意 256 位數字,然后連接結果並刪除前導零。 它使用二進制搜索來找出要刪除的前導零的確切數量,並就地從字符串中刪除前導零。
如果您的 uint 來自最初將字符串編碼為 uint
string(abi.encode(myUint))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.