1

I am using provable service for getting api result from a third party api. The api returns a hex string as a result but I need to convert it to bytes16. I have tried using shift operations for conversion but it doesn't work. Here is my code snippet.

function stringh(string memory x) public view returns(bytes16 fc) {
    bytes memory bts = bytes(x);
    bytes memory f = new bytes(16);
    for(uint k = 0; k < 16; k++) {
        int8 b = p_util_hexdigit[uint8(bts[2*k])];
        f[k] =  byte(b << 4);
        b = p_util_hexdigit[uint8(bts[2*k + 1])];
        f[k] |= byte(b);
    }
    

    assembly {
          fc := mload(add(f, 16))
    }
}

How can I figure out the issue?

1 Answer 1

0

The problem is with mload(add(f, 16)). The first 32 bytes are the length of f, so you have to skip those 32 bytes.

    assembly {
          fc := mload(add(f, 32))
    }

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.