0

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.

1

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 May 21 at 5:21
1

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.

TL;DR;

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 May 22 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 May 22 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/… – Rob Hitchens - B9lab May 22 at 6:00

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.