Serialization of a data string. Central or Data.Binary

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?

+5
source share
2 answers

, " " "\NUL\NUL\NUL\NUL\NUL\NUL\NUL\n. 64 ( , 10), Serialize ByteString. TE.encodeUtf8, putByteString, ( putLazyByteString, ).

+7

put, ByteStrings . :

instance Serialize B.ByteString where
    put bs = do put (B.length bs :: Int)
                putByteString bs
   ...

putByteString . putByteString:

putPassword :: Putter Password
putPassword = putByteString . TE.encodeUtf8 . padText 10
+2

All Articles