I want to turn a string into the 0x byte format inside an entrypoint.

If I try to use sp.pack() the actual value I want gets prepended with pack instructions as detailed here

I'm looking for a runtime equivalent of sp.utils.bytes_of_string() (which can only be used at compile time) that does not prepend anything to the string.

Is it possible to do this inside an entrypoint with smartpy?

  • Are you sure you have to use strings in the first place? All operations on strings are also available on bytes. Jan 21, 2022 at 15:52
  • yes, the entrypoint accepts a normal ipfs cid in string representation, and prepends ipfs:// to the received string in-contract before persisting it as the pointer to the tokens metadata, so it need to turn into its hex representation to work
    – 0x10
    Jan 22, 2022 at 5:15

2 Answers 2


I struggled on this for a while but finally reached a solution.

It's a bit hacky, but you just have to remove the 6 bytes that sp.pack prepends onto the bytes you actually want with sp.slice before using them.

padding = 6
string_length = sp.len(params.meta) + padding + 1

packed_string = sp.slice(

You can use the utilities provided in this repository: tezos-sc-utils

It provides utilities to encode and decode strings.

Here is an example of it working in SmartPy IDE: https://smartpy.io/ide?cid=QmbNMohfAQQqnG98aL1Rj3y5Y4KBJ7Vo9pWuMBfsCHy9mX&k=5ec3179b99a5aab6609e

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.