Skip to content

Instantly share code, notes, and snippets.

@okwme
Last active November 6, 2018 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save okwme/f3a35193dc4eb9d1d0db65ccf3eb4034 to your computer and use it in GitHub Desktop.
Save okwme/f3a35193dc4eb9d1d0db65ccf3eb4034 to your computer and use it in GitHub Desktop.
solidity - uint to hex string
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);
}
@askucher
Copy link

askucher commented Nov 6, 2018

try 0x072b479c08a90414db5f8b2bd684e4323d7d13dd
first 0 is lost

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment