Search code examples
haskellbinarybytestring

Haskell - How do you prepend a bytestring with its length in binary?


I'm in the unfortunate situation where I need to interface with a Java socket API directly in Haskell. The way Java Strings are sent over the wire is with their length is added to the beginning of the string.

For example:

\0\0\0\xBHello World

is the string "Hello World"

I can get the length of the ByteString no problem, but as an Int. I can add a Word32 to the ByteString using Blaze Builder, but I can't find a convenient way to convert the Int to Word32. Is there some convenience method I can't find to do this?

Thanks.


Solution

  • You can convert any integral type with fromIntegral function.