Skip to main content
added 1606 characters in body
Source Link

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)) +
        block.number
    )));

    //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;

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.

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)) +
        block.number
    )));

    //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;
Source Link

How can I convert an element of a byte32 array to number?

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.