Skip to content

Instantly share code, notes, and snippets.

@chriseth
Created June 4, 2015 13:17
Show Gist options
  • Save chriseth/76e6a7897009948d8655 to your computer and use it in GitHub Desktop.
Save chriseth/76e6a7897009948d8655 to your computer and use it in GitHub Desktop.
Solidity decimal conversion
contract c {
uint a;
function charAt(bytes32 b, uint char) returns (bytes1) {
return bytes1(uint8(uint(b) / (2**((31 - char) * 8))));
}
function parseDecimal(bytes32 byteString) returns (uint r) {
uint n = uint(byteString);
for (uint b = 0; b < 32; b ++)
{
var c = uint8(charAt(byteString, b));
if (c < uint8('0') || c > uint8('9'))
return;
r = r * 10 + c - uint8('0');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment