2

If I assign a bytes32 variable such as

bytes32 x = 0xffff;

but then I want to change the last byte (or any byte really) so that x is now 0xffaa, is there any possible way to do this? I want the change to be persistent so that any changes in x are reflected in the blockchain, not just mask into a particular byte and use the result.

1

Yes.

The following code makes x a storage (state) variable of the contract, which can be changed by invoking the changeX function. x is a storage variable whose value will be persisted.

pragma solidity ^0.4.0;
contract C {
    bytes32 public x = 0xffff;

    function changeX() external {
        x = 0xffaa;
    }
}

You can try the code in Remix.

(public makes it easier for you to inspect the value of x.)

  • I should have been more specific in my question, but I'd like to pass a byte and the position of that byte into the function changeX() in your example. How might that be done? – jojeyh Nov 2 '17 at 11:39
  • Please ask a new question. (I think someone else will be able to answer faster than me.) – eth Nov 2 '17 at 19:42
  • This example does not compile – jojeyh Nov 2 '17 at 23:26
  • What doesn't compile? ethereum.github.io/browser-solidity/… – eth Nov 3 '17 at 8:49

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.