De acuerdo con esta pregunta, podría escribir un contrato simple como:
contract SimpleStorage {
bytes input;
function setInput(bytes enterBytes){
input = enterBytes;
}
}
El uso de "0x1234" como parámetro en Remix "no funciona" y almacena "0x307831323334" en la matriz de almacenamiento. Si cambio el código a bytes2
entonces todo funciona según lo previsto con el mismo comando. ¿Cómo puedo hacer lo mismo usando una matriz dinámica?
contract SimpleStorage {
bytes2 input;
function setInput(bytes2 enterBytes){
input = enterBytes;
}
}
código:
contract SimpleStorage
{
bytes input;
function setInput(bytes enterBytes){
input = enterBytes;
}
function getInput()
returns (bytes)
{
return input;
}
function setInputFromHex(string hex_str)
{
input = hexStrToBytes(hex_str);
}
function hexStrToBytes(string hex_str) constant
returns (bytes)
{
//Check hex string is valid
if (bytes(hex_str)[0]!='0' ||
bytes(hex_str)[1]!='x' ||
bytes(hex_str).length%2!=0 ||
bytes(hex_str).length<4)
{
throw;
}
bytes memory bytes_array = new bytes((bytes(hex_str).length-2)/2);
for (uint i=2;i<bytes(hex_str).length;i+=2)
{
uint tetrad1=16;
uint tetrad2=16;
//left digit
if (uint(bytes(hex_str)[i])>=48 &&uint(bytes(hex_str)[i])<=57)
tetrad1=uint(bytes(hex_str)[i])-48;
//right digit
if (uint(bytes(hex_str)[i+1])>=48 &&uint(bytes(hex_str)[i+1])<=57)
tetrad2=uint(bytes(hex_str)[i+1])-48;
//left A->F
if (uint(bytes(hex_str)[i])>=65 &&uint(bytes(hex_str)[i])<=70)
tetrad1=uint(bytes(hex_str)[i])-65+10;
//right A->F
if (uint(bytes(hex_str)[i+1])>=65 &&uint(bytes(hex_str)[i+1])<=70)
tetrad2=uint(bytes(hex_str)[i+1])-65+10;
//left a->f
if (uint(bytes(hex_str)[i])>=97 &&uint(bytes(hex_str)[i])<=102)
tetrad1=uint(bytes(hex_str)[i])-97+10;
//right a->f
if (uint(bytes(hex_str)[i+1])>=97 &&uint(bytes(hex_str)[i+1])<=102)
tetrad2=uint(bytes(hex_str)[i+1])-97+10;
//Check all symbols are allowed
if (tetrad1==16 || tetrad2==16)
throw;
bytes_array[i/2-1]=byte(16*tetrad1+tetrad2);
}
return bytes_array;
}
}
Consideremos que tengo una función cuyo parámetro de entrada es
bytes32
bytes32[ ] // array of type bytes 32
uint8
que se parece a:
function abc(bytes32 id, bytes32[ ] name,uint8 version) returns(bool)
{
//
}
así que ahora, para provocar la función (usando remix) tienes que pasar parámetros que se ven así:
"0x12",["0x1262","0x12","0x12"],8
SCBuergel