With the following code, I want to serialize a Data.Text value to a ByteString. Unfortunately my text is prepended with unnecessary NUL bytes and an EOT byte:
GHCi, version 9.4.4: https://www.haskell.org/ghc/ :? for help
ghci> import qualified Data.Text as T
ghci> import Data.Binary
ghci> import Data.Binary.Put
ghci> let txt = T.pack "Text"
ghci> runPut $ put txt
"\NUL\NUL\NUL\NUL\NUL\NUL\NUL\EOTText"
ghci>
Questions:
- Why are these NUL and EOT bytes generated?
- How can I avoid them in the resulting ByteString?
PS: I the real code I put the length in front of the text
foo :: Text -> ByteString
foo txt = runPut do
putWord32host $ T.length txt
put txt
Text
as well. – IncestNUL
s, so zero length, for one character, the last one is replaced withSOH
which means one, and so on. – Incest