Fix buffer encoding/decoding for payload

We have to use the same encoding for a buffer and calculating its length.