I saw this question already, and several others going the other direction, from int to string, but how do I go specifically from string to a uint256?
I saw this answer has code for uint only, and it makes sense, but what needs to change for uint256?
I am also interested in how error-handling works in this---suppose the string does not contain integers, what is the right way to return an error, and to handle this in the calling function?
Update
Answers below were helpful. So would something like the below give me uint==uint256 and error handling efficiently and idiomatically?
function stringToUint(string s) constant returns ((bool success,uint result)) {
bytes memory b = bytes(s);
uint result = 0;
success = false;
for (uint i = 0; i < b.length; i++) {
if (b[i] >= 48 && b[i] <= 57) {
result = result * 10 + (uint(b[i]) - 48);
success = true;
} else {
result = 0
success = false;
break;
}
}
return (success,result);
}
Update 2
Sorry, one more part of error handling: I understand the max value of uint256 to be:
2^256-1 = 115792089237316195423570985008687907853269984665640564039457584007913129639935
How would I handle correctly rejecting a string that would cause rollover?
eg, would this:
stringToUint("115792089237316195423570985008687907853269984665640564039457584007913129639937")
(which is 2^256+1) rollover to 1?