0

I have bytes32 data which needs to be converted into a string, then use to get value from mapping(string => string)

Trying to convert bytes32 to string using : string(abi.encodePacked(temp))

It is working fine:

enter image description here

enter image description here

Mapping contains key value pair : "har0 => "hello"

mapping

But when I uses the same string(converting from bytes32) in mapping to get the value, it doesn't gives value.

enter image description here

enter image description here

1 Answers1

0

You are trying to decode packed data, which is not possible with abi.decode. As abi.decode can only decode the data that's not packed.

The correct way to decode a bytes input is this

  function convert(bytes memory data) public pure returns (string memory) {
        return abi.decode(data,(string));
    }

And to get the mapping:

function getmap(bytes memory key) public view returns (string memory) {
    return map[convert(key)];
}

try this and it will be working.

In case you really want to work with ancodePacked, you can look into some assembly code for this. Here is a relatable answer : How to decode `encodePacked` data

Zartaj Afser
  • 3,619
  • 1
  • 8
  • 25