Okay, so this code is actually designed to revert when anything but 0, 1, ...9, is entered, but let me explain.
I created this code for general practice. It may not have practical use, but it's intent is to convert a string version of the digits 0 - 9 to the uint version. In testing it, it works perfectly when I enter any string with the characters 0 - 9.
pragma solidity ^0.4.24;
contract ConvertStringToUint { // This version does not limit the number of bytes in the string to 78.
function stringToUint(string _number) public pure returns(uint) {
bytes memory strBytes = bytes(_number);
uint i;
uint digit;
uint result;
for (i = 0; i < strBytes.length; i++) {
if (strBytes[i] == byte(48)) {
digit = 0;
} else if (strBytes[i] == byte(49)) {
digit = 1;
} else if (strBytes[i] == byte(50)) {
digit = 2;
} else if (strBytes[i] == byte(51)) {
digit = 3;
} else if (strBytes[i] == byte(52)) {
digit = 4;
} else if (strBytes[i] == byte(53)) {
digit = 5;
} else if (strBytes[i] == byte(54)) {
digit = 6;
} else if (strBytes[i] == byte(55)) {
digit = 7;
} else if (strBytes[i] == byte(56)) {
digit = 8;
} else if (strBytes[i] == byte(57)) {
digit = 9;
} else {
revert();
}
result = result * 10 + digit;
}
return result;
}
}
However, when I add a letter or any other character, I get an error I didn't expect.
For example, if I enter string "324", it returns the uint "324". But if I enter string "3a24", I get the error:
Error encoding arguments: SyntaxError: Unexpected token a in JSON at position 2
This suggests that it is not even allowing me to enter these characters as a string in the first place, which I don't understand because the input parameter is a string.
To further test this, I changed the line else if (strBytes[i] == byte(49))
to else if (strBytes[i] == byte(97))
, so that now the character "a" will set digit to 0 and the character "0" should cause an error. Sure enough, entering "0" gives me the error:
... VM error: revert. revert The transaction has been reverted to the initial state.
And even at this point, entering "a" still gives me the error:
.. Unexpected token a in JSON at position 1
I've been through it several times and can't account for these error differences. What is going on?