I need to store a value of this kind 0xff0000
or 0x00ff08
(hex colour representation) in solidity smart contract and be able to convert it inside a contract to a string with the same text characters "ff0000"
. I intend to deploy this smart contract on RSK.
My idea was to store those values in a bytes3
or simply uint
variable and to have a pure function converting bytes3
or uint
to corresponding string. I found a function that does the job and working on solidity 0.4.9
pragma solidity 0.4.9;
contract UintToString {
function uint2hexstr(uint i) public constant 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;
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);
}
}
But I need a more recent compiler version (at least 0.8.0). The above function is not working on newer versions.
What's the way to convert bytes
or uint
to a hex string (1->'1',f->'f') what works in Solidity >=0.8.0 ?