Getting one byte from a hash?

VetruvianVetruvian Member Posts: 17
I am trying to get one byte from a bytesXX variable. I hope this can be used to create random numbers based on the current blockhash (a byte32).

I have tried retrieving one byte from the blockhash, but I can not get the solidity to compile. Below is a simple example:
contract Random { bytes1 test; function Random() { test = block.blockhash(0)[31]; } }

The error I get is:
:4:16: Type error: Indexed expression has to be a type, mapping or array (is bytes32) test = block.blockhash(1)[31]; ^----------------^

Is what I am trying to do possible? Is this the right direction to create random numbers, or is it not possible at the moment?

Comments

  • chrisethchriseth Member Posts: 170 ✭✭✭
    edited April 2015
    This is not yet implemented. For now, you have to do it manually using e.g.
    (uint(block.blockhash(1)) / (2**31)) & 0xff
    But please try it first, there are some pitfalls concerning alignment and endianness.
  • VetruvianVetruvian Member Posts: 17
    Thank you for the workaround. I will try it when I get my local test environment up and running again (switching to the dev ppa killed my working setup :()
Sign In or Register to comment.