Created
June 4, 2015 13:17
-
-
Save chriseth/76e6a7897009948d8655 to your computer and use it in GitHub Desktop.
Solidity decimal conversion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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