0

Assuming I decided to ABI encode a very long string (for the purpose of this question I just picked an arbitrary 5-paragraph lorem ipsum text generate by https://www.lipsum.com/feed/html) and get out the following (shortened) abi-encoded string:

00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000b63224c6f72656d20697073756d20646f6c6f722073697420616d65742c20636f6e73656374657475722061646970697363696e6720656c69742e205072616573656e74207068617265747261206d61737361206e657175652c206964206665726d656e74756d207175616d20646170696275732073697420616d65742e2043726173206d6f6c65737469652066656c6973206163206d61737361207072657469756d2072757472756d2e205072616573656e7420616c69717565742c20657820696420617563746f72206f726e6172652c206e65717565206d65747573207665686963756c612070757275732c20657520706f72747469746f72206c6967756c6120747572706973207363656c657269737175652073617069656e2e2051756973717565206d6f6c6c69732075726e612065752073617069656e20657569736d6f642c2073697420616d65742070686172657472612065737420636f6e73656374657475722e20416c697175616d2076756c7075746174652074656d706f72207363656c657269737175652e204e756c6c616d20616320656c656d656e74756d2065782e204e756e632076656c2076756c707574617465206e657175652c2061742076656e656e61746973206a7573746f2e20416c697175616d207365642073656d206c6962657

How do I go about decoding this? I know the leading few hexchars of the string after the padding are there to specify the length, but how does one determine where the hexchars representing the length end and where the actual utf-8 encoding of the string begins?

1

If you are using web3, you can encode it with:

web3.eth.abi.encodeParamter('string', <lorem_ipsum_text>)

You can then decode the output with the following:

web3.eth.abi.decodeParamter('string', <lorem_ipsum_encoded_string>)


Encoding is based on the Contract ABI Specification. It is hard to get through, but these docs have all the answers to your question.

In your example:

0000000000000000000000000000000000000000000000000000000000000020

This is the location of the string (a dynamic parameter). This is the location where string lives, but is not the data itself.

0000000000000000000000000000000000000000000000000000000000000b63224c6f72656d20697073756d20646f6c6f722073697420616d65742c20636f6e73656374657475722061646970697363696e6720656c69742e205072616573656e74207068617265747261206d61737361206e657175652c206964206665726d656e74756d207175616d20646170696275732073697420616d65742e2043726173206d6f6c65737469652066656c6973206163206d61737361207072657469756d2072757472756d2e205072616573656e7420616c69717565742c20657820696420617563746f72206f726e6172652c206e65717565206d65747573207665686963756c612070757275732c20657520706f72747469746f72206c6967756c6120747572706973207363656c657269737175652073617069656e2e2051756973717565206d6f6c6c69732075726e612065752073617069656e20657569736d6f642c2073697420616d65742070686172657472612065737420636f6e73656374657475722e20416c697175616d2076756c7075746174652074656d706f72207363656c657269737175652e204e756c6c616d20616320656c656d656e74756d2065782e204e756e632076656c2076756c707574617465206e657175652c2061742076656e656e61746973206a7573746f2e20416c697175616d207365642073656d206c6962657...

This is the data itself. Decoded, this produces the Lorem Ipsum text.

| improve this answer | |
  • Thank you, but my question was more specific as to when one already has said data you show, how one would go about decoding such a long string, as the way i understood abi is that you'd first parse out how long your string is and then use that to decode the remaining hexchars from utf8. However with such a long text, how would one know if the length is represented by (in this example) b632, b6322, or b63224 since one can't know if a pair of hexchars (such as the trailing 24 in the last example) represents part of the length or a utf8 character. – Nicolas Schapeler Mar 31 at 16:23
  • @NicolasSchapeler The length is 32 bytes (64 hex digits) 0000000000000000000000000000000000000000000000000000000000000b63. So length is 2915. – Ismael Mar 31 at 17:34
  • That't the answer I was looking for, thank you so much! – Nicolas Schapeler Apr 1 at 11:12

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.