¿Cómo concatenaría un número entero en una cadena usando solidez?
Por ejemplo, digamos que tengo lo siguiente:
uint myInteger = 12
¿Cómo puedo crear la siguiente cadena "My integer is: 12"
con 12 siendo el valor de myInteger.
En otras palabras tener,"My integer is: " + myInteger
Prueba lo siguiente:
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);
}
}
Aquí está la pantalla de solidez del navegador que muestra el funcionamiento de este algoritmo:
explicit conversion from uint256 to bytes1 is unsupported
para la líneareversed[i++] = byte(48 + remainder)
byte(uint8(48 + remainder))
Esta es mi primera respuesta aquí :)
toString(value) : Convierte un uint256 a su representación decimal de cadena ASCII.
se verá así:
pragma solidity ^0.8.12;
import "@openzeppelin/contracts/utils/Strings.sol";
contract NumToStr {
function testConcat(uint256 num1) public pure returns (string memory) {
return string.concat("The number is: ",Strings.toString(num1), ", Cool!!");
}
}
hay otros métodos en Strings.sol de openzeppelin como :
toHexString(value)
toHexString(value, length)
toHexString(addr)
Puede leer más sobre la biblioteca Strings en los documentos de openzeppelin aquí :
En lugar de string.concat() , también podemos usar abi.encodePacked(str1, str2) , que devuelve bytes (ver aquí ). Luego podemos encasillar los bytes en una cadena con string(bytesVal) :
function testConcat(uint256 num1) public pure returns (string memory) {
return string(abi.encodePacked("The number is: ",Strings.toString(num1), ", Cool!!"));
}
Pero sé que este enfoque es un poco raro.
function strConcat(string _a, string _b, string _c, string _d, string _e) internal returns (string){
bytes memory _ba = bytes(_a);
bytes memory _bb = bytes(_b);
bytes memory _bc = bytes(_c);
bytes memory _bd = bytes(_d);
bytes memory _be = bytes(_e);
string memory abcde = new string(_ba.length + _bb.length + _bc.length + _bd.length + _be.length);
bytes memory babcde = bytes(abcde);
uint k = 0;
for (uint i = 0; i < _ba.length; i++) babcde[k++] = _ba[i];
for (i = 0; i < _bb.length; i++) babcde[k++] = _bb[i];
for (i = 0; i < _bc.length; i++) babcde[k++] = _bc[i];
for (i = 0; i < _bd.length; i++) babcde[k++] = _bd[i];
for (i = 0; i < _be.length; i++) babcde[k++] = _be[i];
return string(babcde);
}
function strConcat(string _a, string _b, string _c, string _d) internal returns (string) {
return strConcat(_a, _b, _c, _d, "");
}
function strConcat(string _a, string _b, string _c) internal returns (string) {
return strConcat(_a, _b, _c, "", "");
}
function strConcat(string _a, string _b) internal returns (string) {
return strConcat(_a, _b, "", "", "");
}
q9f