21

According to this question I could write a simple contract like:

contract SimpleStorage {
  bytes input;
  function setInput(bytes enterBytes){
    input = enterBytes;
  }
}

Using "0x1234" as a parameter in Remix "does not work" and stores "0x307831323334" in the storage array. If I change the code to bytes2 then everything works as intended with the same command. How can I do the same using a dynamic array?

contract SimpleStorage {
  bytes2 input;
  function setInput(bytes2 enterBytes){
    input = enterBytes;
  }
}
22
+50
  1. You can pass bytes parameters in Remix or browser-solidity as the array of single bytes, for example ["0x00","0xaa", "0xff"] is equivalent to "0x00aaff"
  2. Do not know why, but the Remix IDE and browser-solidity interprets "0xaabb11..." as string. For development and testing purpose on private or test net you can use the code below. Function hexStrToBytes will do the conversion. You can use its result, as it is demonstrated in setInputFromHex function

code:

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;
    }
}
  • This just sort of hangs saying "waiting to be mined". It might be that my bytes string is too long – ZMitton Mar 30 '17 at 19:39
  • @ZMitton if you need to store something to blockchain, for example when you execute setInputFromHex, you need mining process. It is not the problem of this contract - it is rule. If you work on your own node - start mining. Function hexStrToBytes does not need mining process (already, code was edited). – Alexey Barsuk Mar 30 '17 at 20:22
  • I awarded the bounty based on solution 1. This solves the issue of inputting bytes from remix/browser-solidity. Thank you – ZMitton Mar 30 '17 at 20:33
  • @ZMitton Could you please mark answer as "answered" – Alexey Barsuk Mar 31 '17 at 10:33
  • If anyone needs a quick way to do this for debugging I made this little util to make it faster: random bytes generator for remix – valem Nov 23 '18 at 8:30
2

let us consider i have function whose input parameter is

bytes32

bytes32[ ] // array of type bytes 32

uint8

which looks like:

 function abc(bytes32 id, bytes32[ ] name,uint8 version) returns(bool)
    {  
        //
    }

so now,to provoke the function (using remix) you have to pass parameters which looks like:

"0x12",["0x1262","0x12","0x12"],8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.