[Haskell-beginners] Different ByteStrings, what now?

Daniel Fischer daniel.is.fischer at googlemail.com
Thu Jun 2 15:30:40 CEST 2011


On Thursday 02 June 2011 15:23:40, Raphael Päbst wrote:
> Hello once again!
> My forays into Haskell are once again stopped by my weak Haskell Fu.
> I am working with ProtocolBuffers and so far everything worked fine,
> but now I have encountered a problem. I am trying to serialize a
> message that contains, among other things, a blob of binary data. My
> problem now is to get that binary data into the message.
> The message requires the data type
> Text.ProtocolBuffers.Header.ByteString, whereas my data is of the type
> Data.ByteString. How can I turn one into the other or get my data into
> the right format in te first place?
> I tried to find a function to solve this, but I couldn't find it in
> the ProtoBuff documentation.

I don't know ProtoBuff, but I suspect that one uses lazy ByteStrings [there 
are only lazy and strict ByteStrings in the bytestring package], in which 
case you could simply transform your strict ByteString via

strictToLazy bs = Data.ByteString.Lazy.fromChunks [bs]

into a lazy ByteString (the reverse transformation would be
Data.ByteString.concat . Data.ByteString.Lazy.toChunks, but it might be 
better to work with lists of strict ByteStrings as delivered by toChunks).

> 
> Thanks
> 
> Raf



More information about the Beginners mailing list