Converting string to bytes

The following question has been asked on Telegram:

How to convert string to bytes

It depends on what you mean by that.


On Tezos, the way to convert data into a serial representation is called “pack”.
You can convert a string to it’s packed representation with sp.pack(my_string). The type of the result is sp.bytes.

About serial representation, you can have a look at How to UNPACK into a specific type using tezos client? - Tezos Stack Exchange

Serialization is the process of converting a tree-like structure into a linear representation. In our case the tree-like structure is Micheline and the linear representation is a sequence of bytes (usually written in hexadecimal notation with the 0x prefix). Deserialization is, as the name suggests, the opposite transformation from byte sequences to Micheline.

Bytes encoding

In pure Python (outside of a @sp.module) you can transform a Python string to it’s utf-8 encoding by doing: "0x" + s.encode("utf-8").hex(). This will provide a Python string in the form “0x…”. You can create a SmartPy bytes value from it with sp.bytes("0x" + s.encode("utf-8").hex()).

This process is different from the previous one:

  • You can only do it off-chain
  • It’s not representing a Michelson value (it’s not a representation of a tree structure with types in it).
  • Resulting bytes are different

When using which

If you want to encode the metadata of a Smart contract or a token contract, the standard says you should use the second. You can use sp.scenario_utils.bytes_of_string(x) as an helper.

Otherwise, you probably want to use pack and unpack as you can deal with it on-chain (and off-chain too).