0

I have a string array stored in memory as

https://cloudflare-ipfs.com/ipfs/QmSXDsFeNaPa3CJKmn8WKBnA421Zv5r3Ra8n71LZhvEi9s

This should convert to 68747470733a2f2f636c6f7564666c6172652d697066732e636f6d2f697066732f516d5358447346654e61506133434a4b6d6e38574b426e413432315a763572335261386e37314c5a687645693973

As can be seen here https://codebeautify.org/hex-string-converter.

Instead though I'm see 4f68747470733a2f2f636c6f7564666c6172652d697066732e636f6d2f697066732f516d5358447346654e61506133434a4b6d6e38574b426e413432315a763572335261386e37314c5a6876456939

->

Ohttps://cloudflare-ipfs.com/ipfs/QmSXDsFeNaPa3CJKmn8WKBnA421Zv5r3Ra8n71LZhvEi9

Only difference is in the first set of hex, it ends with 73 as where the second set begins with 4f.

Using

    function string_tobytes(string memory s) public pure returns (bytes memory){
        bytes memory b3 = bytes(s);
        return b3;
    }

Any help on how to handle this would be appreciated!!

0

Issue was that I was converting from cpp serialized std::string message which contains a LEB128 encoded length prefix, thus needed to add back for deserialization:

// "SPDX-License-Identifier: UNLICENSED"
pragma solidity ^0.8.0;

contract strings {
    
    function string_tobytes(string memory s) public pure returns (bytes memory){
        bytes memory message = abi.encodePacked(
            uint8(bytes(s).length),
            bytes(s)
        );
        return message;
    }
}

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.