-2

enter image description here

The code snippet I have included Converts an ASCII string value into an uint as long as the string itself is a valid unsigned integer and I am getting errors on line "assert(uint8(_bytesValue[i]) >= 48 && uint8(_bytesValue[i]) <= 57);" and on line "_ret += (uint8(_bytesValue[i]) - 48)*j;" as given in the screenshot of the code.

function parseInt(string memory _value) 
    public
    returns (uint _ret) {
    bytes memory _bytesValue = bytes(_value);
    uint j = 1;
    for(uint i = _bytesValue.length-1; i >= 0 && i < _bytesValue.length; i--) 
    {
        assert(uint8(_bytesValue[i]) >= 48 && uint8(_bytesValue[i]) <= 57);
        _ret += (uint8(_bytesValue[i]) - 48)*j;
        j*=10;
    }
}
  • 3
    Please share your code and the errors you get (not a screenshot). – smarx Mar 9 at 8:00
  • Ok... i have edited the body... please look at it – PREETAM Mar 11 at 6:40
1

When reading one element from a bytes, you get a bytes1.

You should probably cast your bytes1 to a uint8

1

You can cast a bytes1 to a uint8 and then compare. For example:

bytes memory _bytesValue = bytes('yo yo');
assert(uint8(_bytesValue[2]) >= 4);
uint _ret = uint8(_bytesValue[2]) - 4;
  • It solved the errors... Thank you so much... – PREETAM Mar 11 at 6:41
  • You should probably choose this answer then. – Mikhail Vladimirov Mar 27 at 11:18
  • There is no byte1 type in Solidity. It is named bytes1. – Mikhail Vladimirov Mar 27 at 11:20
  • ^ fixed, thanks – Hardly Difficult Mar 28 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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