Last active
November 6, 2018 21:52
-
-
Save okwme/f3a35193dc4eb9d1d0db65ccf3eb4034 to your computer and use it in GitHub Desktop.
solidity - uint to hex string
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
function uint2hexstr(uint i) internal pure returns (string) { | |
if (i == 0) return "0"; | |
uint j = i; | |
uint length; | |
while (j != 0) { | |
length++; | |
j = j >> 4; | |
} | |
uint mask = 15; | |
bytes memory bstr = new bytes(length); | |
uint k = length - 1; | |
uint numStart = 48; | |
uint letterStarn = 65; | |
while (i != 0){ | |
uint curr = (i & mask); | |
bstr[k--] = curr > 9 ? byte(55 + curr ) : byte(48 + curr); // 55 = 65 - 10 | |
i = i >> 4; | |
} | |
return string(bstr); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
try 0x072b479c08a90414db5f8b2bd684e4323d7d13dd
first 0 is lost