I have a function that creates random numbers with a maximum value that you gives to it, the function was made to work with any uint256, and using % operator it returns a value between 0 and the maxValue that you gave to it. How could I cast from uint256 (the number that the function returns) to a uint8 for example... I now in this case the funtcion is giving a value that can be saved on a uint8, but looks like solidity doesn't.

Thank you

1 Answer 1


You can just cast it:

  • the thing is that I have a function that returns a uint256, I know it fits on a uint8, but solidity is not sure Jul 14, 2018 at 19:45
  • What do you mean? Do you get some sort of error when you perform the cast? Could you share the code you're using that doesn't work?
    – user19510
    Jul 14, 2018 at 19:46
  • mmm I am sorry, I tried again, and you were totally right, my mistake was not using the () properly on the cast, no in the operation... Jul 14, 2018 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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