I am implementing a protocol that indicates that a string representing a password should be serialized in a field of 10 bytes in length with a fixed length. I am using Data.Cereal to complete this task. Here my most recent ones go for this:
padText :: Int -> Text -> Text
padText fieldLen = T.justifyLeft fieldLen '\NUL'
putPassword :: Putter Password
putPassword = put . TE.encodeUtf8 . padText 10
put by ByteStrings adds an extra 8-byte chunk to the front of what it encodes:
runPut $ putPassword "Friend"
lead to:
"\NUL\NUL\NUL\NUL\NUL\NUL\NUL\nFriend\NUL\NUL\NUL\NUL"
I do not need an extra piece. Why do they do this?
Does anyone know how to serialize only 10 source bytes?
source
share