I need to compare a bytes32 value with a string. In the below example I need to check if QRVal==QRScanVal.

Eg: bytes32 QRVal="0xc59aa6848d32d073bdcaa041b2735d3e4546d0836498e30de7c4c64c50933a33";

string memory QRScanVal="0xc59aa6848d32d073bdcaa041b2735d3e4546d0836498e30de7c4c64c50933a33";

I'm a newbie with solidity any help would be appreciated.

3 Answers 3


Unless there is a very strong argument and the situation is unavoidable I would recommend that you avoid this conversion in the contract.

It is perfectly reasonable to insist that client-side software accommodate the contract and not the other way around. A partial list of reasons:

  1. Contracts are (generally) immutable. This raises the importance of QA. A first line of defense is maximum simplicity. The conversion function may indeed be okay (or not) but it certainly is a lot for reviewers/auditors/attackers to think about.
  2. Gas cost. All those ops will be replicated by all full nodes and a significant cost to everyone who uses the contract. The conversion function is well-suited to off-chain preprocessing in clients/servers where processing capacity is plentiful and nearly free.

I can think of a case where the inputs come from two different contracts and you want the comparison to happen on-chain.


If it is possible to avoid this, then you should avoid it.

Hope it helps.

  • Thank you, this helps. I will think of a redesign.
    – Smi GM
    Commented May 22, 2019 at 5:26
  • "I can think of a case where the inputs come from two different contracts and you want the comparison to happen on-chain."- this is exactly the scenario we have here. QRScanVal is the value sent for comparison to contract, after we scan from the QRCode of a retail product.
    – Smi GM
    Commented May 22, 2019 at 5:34
  • Welcome to the site. If you found the answers helpful please support the site with votes the useful answers. ethereum.meta.stackexchange.com/questions/1/… Commented May 22, 2019 at 6:00

You can convert bytes32 into string and then compare it Here is the link to how to convert bytes32 into string

How to convert a bytes32 to string

Here is the link to compare two string

how to compare strings in solidity?

Hope this will work for you

  • Thanks Rishabh for your input. I did tried the snippet however it did not work for me. the QRVal is a SHA256. Whereas my QRScanVal, is a scanned string value coming in from a QRCode on a product. (The solidity code is being used on Azure workbench, if the input helps.)
    – Smi GM
    Commented May 21, 2019 at 5:21

You can convert your address to string as below,

string memory canAddress = toAsciiString(QRVal);

//Functions to convert the address to string
function toAsciiString(address x) public returns (string memory) {
        bytes memory s = new bytes(40);
        for (uint256 i = 0; i < 20; i++) {
            bytes1 b = bytes1(uint8(uint256(uint160(x)) / (2**(8 * (19 - i)))));
            bytes1 hi = bytes1(uint8(b) / 16);
            bytes1 lo = bytes1(uint8(b) - 16 * uint8(hi));
            s[2 * i] = char(hi);
            s[2 * i + 1] = char(lo);
        return string(abi.encodePacked("0x", s));

function char(bytes1 b) public returns (bytes1 c) {

    if (uint8(b) < 10) return bytes1(uint8(b) + 0x30);
    else return bytes1(uint8(b) + 0x57);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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