The array contains an SHA256 hash. I want to add up 2 hexadecimal values, and later multiply them by 1.125 (only keeping integer). The resulted number would represent an alphanumerical character, it would be random.

"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" (length is 36)

So I want to pick random letters from this string, and what I have is a SHA256 hash.

function GenerateSixDigitCode() private view returns (bytes32) {
    uint8[6] TwoHashDigit;                     //We add up 2 hex digit, than multiply it by 1.125 (32 to 36 conversion)

    //The CryptoZombies tutorial mentioned that this is not the best source of randomness, but in some cases this is sufficient.
    //This will be a ticket buying app just for learning purposes so it should be good enough 
    bytes32 seed = (keccak256(abi.encodePacked(
        block.timestamp + block.difficulty +
        ((uint256(keccak256(abi.encodePacked(block.coinbase)))) / (now)) +
        block.gaslimit + 
        ((uint256(keccak256(abi.encodePacked(msg.sender)))) / (now)) +

    //The user should get a 6-digit code, like this: X4C9BA
    //I'm trying to generate the 6-digit code from sha256

    TwoHashDigit[0] = (seed[0] + seed[1]) * 1.125;
    TwoHashDigit[1] = (seed[2] + seed[3]) * 1.125;
    TwoHashDigit[2] = (seed[4] + seed[5]) * 1.125;
    TwoHashDigit[3] = (seed[6] + seed[7]) * 1.125;
    TwoHashDigit[4] = (seed[8] + seed[9]) * 1.125;
    TwoHashDigit[5] = (seed[10] + seed[11]) * 1.125;

    //TwoHashDigit should contain random numbers now, in the range of 0-36

    //Later I would map numbers to alphanumeric characters, using this string  alpha = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    //For example alpha[10] == 'A'
    //alpha[20] == 'K'

The error I get:

contracts/ticketpurchaser.sol:43:27: TypeError: Operator * not compatible with types bytes1 and rational_const 9 / 8
        TwoHashDigit[5] = (seed[10] + seed[11]) * 1.125;
  • Please clarify your question and provide the relevant piece of code. Why do you have an array which consists of a single element? What do you mean "add up 2 hexadecimal values"? Add to what??? What do you mean "later multiply them by 1.125"? What does "later" even mean in this context? Do you want to multiply the result by 1.125, or just each one of these two values (in which case, what's stopping you from doing that)? What do you mean "represent an alphanumerical character"??? The requirement at the last line is so obscure I'm not even sure what to ask about it. Please clear it up! Commented Mar 18, 2020 at 14:31

1 Answer 1


Change every occurrence of this:

(seed[x] + seed[y]) * 1.125

To this:

uint8((uint16(seed[x]) + uint16(seed[y])) * 9 / 8)

And since you have many of those, you may as well implement it in a function:

function combine(bytes1 x, bytes1 y) private pure returns (uint8) {
    return uint8((uint16(x) + uint16(y)) * 9 / 8);
  • that didn't change the error message Type bytes1 is not implicitly convertible to expected type uint8. TwoHashDigit[4] = (seed[8] + seed[9]) * (9/8);
    – Imestin
    Commented Mar 18, 2020 at 15:49
  • @Imestin: It did change the error message, which according to your question is TypeError: Operator * not compatible with types bytes1 and rational_const 9 / 8!!! Anyway, let me extend the answer... Commented Mar 18, 2020 at 15:52
  • If I omit the parenthesis from (9/8) it compiles.
    – Imestin
    Commented Mar 18, 2020 at 16:04
  • @Imestin: I never wrote anywhere that you should use parenthesis around 9/8! In fact, you definitely should, because that's simply gonna be 1, which is obviously not what you want. You're welcome. Commented Mar 18, 2020 at 16:11

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.