2

I have a function in my contract and it have bytes1[4] array parameter and returns.

function foo(bytes1[4] memory _mdata) public pure returns(bytes1[4]){
    bytes1[4] memory result;
    uint8 i;
    for(i=0 ; i<4 ; i++){
        result[i] = _mdata[i];
    }
    return result;
}

And in my truffle test javascript, it have something like..

var myData = [0x01, 0x02, 0x03, 0x1f];
it("test", async() => {
    let instant = await myArithmetic.deployed();
    let result = await instant.foo(myData);
    console.log(result);

I got [ '0x10', '0x20', '0x30', '0x1f' ] Is this because of different endian between javascript and solidty compiler? But why the last one returned correctly??

  • I know endian is related to the byte order not 4-bits. Is it bug on truffle or solidity? – Kronos May 2 '18 at 12:34
  • If you pass ["0x01", "0x02", "0x03", "0x1f"] does it work? I often have problems with javascript function when you pass a number but it expect a string. To be safe everything bytesXX seems to require a string. – Ismael May 3 '18 at 16:32
  • It works fine with ["0x01", "0x02", "0x03", "0x1f"] !! Thank you. – Kronos May 4 '18 at 5:40

Your Answer

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

Browse other questions tagged or ask your own question.