3

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.

jojeyh
  • 769
  • 3
  • 8

1 Answers1

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.)

eth
  • 85,679
  • 53
  • 285
  • 406
  • 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 02 '17 at 11:39
  • Please ask a new question. (I think someone else will be able to answer faster than me.) – eth Nov 02 '17 at 19:42
  • This example does not compile – jojeyh Nov 02 '17 at 23:26
  • What doesn't compile? https://ethereum.github.io/browser-solidity/#version=soljson-v0.4.18+commit.9cf6e910.js&optimize=undefined&gist=c8937de1d4e2afbfd36a1255377700a3 – eth Nov 03 '17 at 08:49