Solidity
Solidity將uint連接成一個字元串?
如何使用solidity將整數連接成字元串?
例如,假設我有以下內容:
uint myInteger = 12
如何創建以下字元串
"My integer is: 12"
,其中 12 是 myInteger 的值。換句話說,
"My integer is: " + myInteger
嘗試以下操作:
pragma solidity ^0.4.4; contract TestIntToString { string public result; function TestIntToString() { // result = uintToString(12345678901234567890); result = appendUintToString("My integer is: ", 1234567890); } 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); for (uint j = 0; j < i; j++) { s[j] = reversed[i - 1 - 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); 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 - 1 - j]; } str = string(s); } }
這是顯示此算法工作原理的 Browser Solidity 螢幕: