1

Pretty simple code here:

pragma solidity ^0.4.13;

contract Oracle{
    event Print(string _name, bytes32 _value);
    bytes32 key;

    function Store(bytes32 _key) returns (bytes32) {
        key = _key;
        Print("Key",_key);
        return _key;
    }

}

Now I'm trying to store the value 1 as a key.

Using remix,

entered key: 1
printed key: 0x0100000000000000000000000000000000000000000000000000000000000000

entered key: "0x01"
printed key: 0x0100000000000000000000000000000000000000000000000000000000000000

Now a weird one:

entered key: "0x1"
printed key: 0x0100000000000000000000000000000000000000000000000000000000000000

And also weird:

entered key: "1"
printed key: 0x3100000000000000000000000000000000000000000000000000000000000000

And also super weird...when using NodeJS to push a raw transaction:

Entered:1
Printed key: 0x1000000000000000000000000000000000000000000000000000000000000000

So the question, why does "0x1" and "0x01" and 1 all give me the same answer on remix and not on node? And why is 1 when in quotes something different?

marked as duplicate by Ismael, Richard Horrocks, Achala Dissanayake, Badr Bellaj Sep 18 '17 at 23:02

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

As per your examples:

entered key: 1 printed key: 0x0100000000000000000000000000000000000000000000000000000000000000

Bytes are stored as an array of 256bit values (in your case of size 32 bytes) so as you're storing the value of uint 1 in the first byte of the array which is simply 0x01 and the rest of the array is null.

entered key: "0x01" printed key: 0x0100000000000000000000000000000000000000000000000000000000000000

In solidity "0x01" will get cast to its string equivalent as it's a valid hex value for a 256 bit uint

entered key: "0x1" printed key: 0x0100000000000000000000000000000000000000000000000000000000000000

Same as above as 0x01 is the same as 0x1, just like 0x001 and 0x000001, they are all the integer value of 1

entered key: "1" printed key: 0x3100000000000000000000000000000000000000000000000000000000000000

This is the ascii hex value of the string "1" which is 0x31

Entered:1 Printed key: 0x1000000000000000000000000000000000000000000000000000000000000000

I've never tried with NodeJS so I can't answer this one sorry. Hope I answered your other questions though.

  • Thanks James, good stuff. It's slightly annoying odd to the non-bit familiar, but at least it makes a little more sense now – thefett Sep 18 '17 at 19:06

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