Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@askucher 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