I have this function that converts a unit to a hex string:
function uintToHexString(uint i) public pure returns (string memory) {
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;
while (i != 0) {
uint curr = (i & mask);
bstr[--k] = curr > 9 ?
bytes1(uint8(55 + curr)) :
bytes1(uint8(48 + curr)); // 55 = 65 - 10
i = i >> 4;
}
return string(bstr);
}
The above function works as intended. For example, when I pass in the number 255 it returns the string "FF".
Now, I am having trouble creating a function that does the reverse- takes the string "FF" (or any hex string in general) and returns the corresponding uint value.