0

In the Ethereum wiki RLP page,

If a string is more than 55 bytes long, the RLP encoding consists of a single byte with value 0xb7 plus the length in bytes of the length of the string in binary form, followed by the length of the string, followed by the string. For example, a length-1024 string would be encoded as \xb9\x04\x00 followed by the string. The range of the first byte is thus [0xb8, 0xbf].

First of all, how 'dog' is over 55 bytes in python3? I test 'dog' and python 3 show me it is 56 bytes so I can understand 'dog' is 0x83. The funny thing for me is cat&dog is still 56 bytes.

The second thing is, in python3, len('string...') = 1024 shows 1080 bytes. I have no idea how it encoded to \xb9\x04\x00.

Could you explain how it becomes \xb9\x04\x00?

0

"dog" is 3 bytes: "d", "o", and "g". Without knowing what you're doing in Python, I can't explain why you would see something different.

As to why something of length 1024 starts with \xb9\x04\x00, the portion of the page you quoted explains it:

  • The length is more than 55 bytes, so the first byte is 0xb7 plus the number of bytes needed to represent the length. The length is 1024, which requires two bytes to represent. (The range of a single byte is 0-255.) So 0xb7 + 2 = 0xb9.
  • After the first byte is the length of the string. The length of the string is 1024, which in hexadecimal is 0x400, so the next two bytes are 0x40 and 0x00.
  • After that would be the string itself.

So 0xb94000, or \xb9\x40\x00, is the correct prefix for a length-1024 string.

| improve this answer | |
  • Why is it \xb9\x04\x00 and not \xb9\x00\x04? I thought lengths were supposed to be big-endian, and that meant the 4 in 4 * 256^1 + 0 * 256^0 was supposed to go on the end? – Alex Knauth Jan 21 '19 at 0:37
  • They are big-endian. In big-endian, the most significant bits go first. – user19510 Jan 21 '19 at 0:40

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.