В соответствии с этим вопросом я мог бы написать простой контракт, например:
contract SimpleStorage {
bytes input;
function setInput(bytes enterBytes){
input = enterBytes;
}
}
Использование «0x1234» в качестве параметра в Remix «не работает» и сохраняет «0x307831323334» в массиве хранения. Если я изменю код на bytes2
то все будет работать как задумано с той же командой. Как я могу сделать то же самое, используя динамический массив?
contract SimpleStorage {
bytes2 input;
function setInput(bytes2 enterBytes){
input = enterBytes;
}
}
код:
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;
}
}
давайте рассмотрим, что у меня есть функция, входной параметр которой
bytes32
bytes32[ ] // array of type bytes 32
uint8
который выглядит так:
function abc(bytes32 id, bytes32[ ] name,uint8 version) returns(bool)
{
//
}
так что теперь, чтобы спровоцировать функцию (используя ремикс), вам нужно передать параметры, которые выглядят так:
"0x12",["0x1262","0x12","0x12"],8
СКБюргель