1

I am working on a contract in which I have a requirement to convert an integer to hexadecimal number. Suppose I have a number 234 its hexadecimal format is EA so how would I perform this in solidity. Please, someone help me with this.

  • 1
    Hi there. Numbers are numbers. You mean you want to convert its human-readable form from decimal to hexadecimal? Meaning you're dealing with the string representation of the number inside the contract? Could you not just do this outside the contract, which would be far easier? – Richard Horrocks May 3 '18 at 17:38
  • Yes Richard you got it right I need to convert it from decimal to hexadecimal. Is it possible to convert inside contract? – Vikas May 3 '18 at 17:42
  • But you mean as a string? What are the input and output types? – Richard Horrocks May 3 '18 at 17:56
  • Input type would be number like 234 and output type would be string like EA(hexadecimal of 234) – Vikas May 3 '18 at 17:59
  • Sounds like a waste of gas. Why would you pay to maintain a string representation of an integer on the blockchain??? – goodvibration May 3 '18 at 19:30
1

My attempt to convert address to hex string:

function addressToString(address _addr) public pure returns(string) {
    bytes32 value = bytes32(uint256(_addr));
    bytes memory alphabet = "0123456789abcdef";

    bytes memory str = new bytes(42);
    str[0] = '0';
    str[1] = 'x';
    for (uint i = 0; i < 20; i++) {
        str[2+i*2] = alphabet[uint(value[i + 12] >> 4)];
        str[3+i*2] = alphabet[uint(value[i + 12] & 0x0f)];
    }
    return string(str);
}
0

In my opinion that sort of operation is almost always avoidable and it should be avoided even at the price of great inconvenience to client software.

There are plenty of reasons. Two that stand out:

  1. Inside the contract, compute resources are scarce and expensive and errors may be catastrophic and irreversible. Maximize simplicity.
  2. Client-side, compute resources are plentiful and cheap and any errors or oversights can be resolved with ease. There is nothing unreasonable about pushing responsibilities to software clients.

Carry on as though client convenience is of no concern at all. Some elaboration on the idea: https://medium.com/solidified/the-joy-of-minimalism-in-smart-contract-design-b67fb4073422

Hope it helps.

0

It is not difficult to write a simple conversion routine, but IMHO like others have said conversion to strings can be expensive so unless you have a very good reason it is better to avoid it.

function toHexDigit(uint8 d) pure internal returns (byte) {
    if (0 <= d && d <= 9) {
        return byte(uint8(byte('0')) + d);
    } else if (10 <= uint8(d) && uint8(d) <= 15) {
        return byte(uint8(byte('a')) + d - 10);
    }
    // revert("Invalid hex digit");
    revert();
}

function toHexString(uint a) public pure returns (string) {
    uint count = 0;
    uint b = a;
    while (b != 0) {
        count++;
        b /= 16;
    }
    bytes memory res = new bytes(count);
    for (uint i=0; i<count; ++i) {
        b = a % 16;
        res[count - i - 1] = toHexDigit(uint8(b));
        a /= 16;
    }
    return string(res);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.