bytestring

How to force strict evaluation of a sequence of ByteString

≡放荡痞女 提交于 2019-12-24 10:14:46
问题 I have the following Haskell type definition: import Data.Sequence(Seq, length) import Data.ByteString.UTF8(ByteString) type StringSeq = Seq ByteString I have expressions of type StringSeq for which I would like to force strict evaluation with deepseq . So I need to define instances of NFData . I did the following: import Control.DeepSeq(NFData, deepseq) instance NFData ByteString instance NFData a => NFData (Seq a) where rnf s = rnf (length s) So I compute the length of a sequence to force

ByteString representation of a Double conversion

偶尔善良 提交于 2019-12-24 09:12:35
问题 I'm writing my own WAVE parser that uses Conduit so I can stream values, one by one, through a pipeline. I get a sample from a .wav file via hGet (n is the number of bytes per sample for that wav file): bytes <- hGet h n This gives me a ByteString with a representation of the Double value of the sample. E.g.: "\131\237\242" represents -0.10212671756744385 "g\238\242" represents -0.10209953784942627 "\215\238\242" represents -0.10208618640899658 . The possible values are between -1 and +1. Is

Haskell, Aeson - no instance for (ToJSON ByteString)

不羁岁月 提交于 2019-12-24 01:27:29
问题 So happy making it this far, encountered a new hurdle: Got this code made to be encoded to JSON. However no matter when type I use as an instance, the compiler complains. Now I am obviously doing something wrong, but it is exactly what is in the documentation (when using DeriveGeneric obviously). {-# LANGUAGE OverloadedStrings, DeriveGeneric #-} import Data.Aeson import Data.Text as T import Data.ByteString.Lazy as B import Data.ByteString.Lazy.Char8 as BC import GHC.Generics -- decode ::

How to create two ByteStrings calling this external library API?

冷暖自知 提交于 2019-12-23 10:18:14
问题 I'm currently writing bindings to a cryptographic library that exposes a function for generating keypairs: const size_t PUBLICKEYBYTES = 32; const size_t SECRETKEYBYTES = 32; int random_keypair(unsigned char pk[PUBLICKEYBYTES], unsigned char sk[SECRETKEYBYTES]); This function randomly generates a secret key, computes the corresponding public key and puts the results in pk and sk . When just returning one ByteString I've found that the easiest way is to use create :: Int -> (Ptr Word8 -> IO ()

How to convert a ByteString to an Int and dealing with endianness?

谁说我不能喝 提交于 2019-12-23 10:15:57
问题 I need to read a binary format in Haskell. The format is fairly simple: four octets indicating the length of the data, followed by the data. The four octets represent an integer in network byte-order. How can I convert a ByteString of four bytes to an integer? I want a direct cast (in C, that would be *(int*)&data ), not a lexicographical conversion. Also, how would I go about endianness? The serialized integer is in network byte-order, but the machine may use a different byte-order. I tried

How do I convert a unicode header to byte string in Flask?

二次信任 提交于 2019-12-22 11:08:14
问题 I have a flask app that I've been able to get running on my development server. However, when I try to run the same app under mod_wsgi I get an error: TypeError: expected byte string object for header name, value of type unicode found I've tried to convert the headers many different ways but I'm getting the same error: for k,v in dict(request.headers).iteritems(): response.headers[k.encode('latin-1')] = v.encode('latin-1') I've also tried the following but get the same exact error: .encode(

Haskell: Does ghci show “Chunk .. Empty”?

只谈情不闲聊 提交于 2019-12-22 04:54:30
问题 Learn You a Haskell has a code example like this: ghci> B.pack [99,97,110] Chunk "can" Empty ( B stands for Data.ByteString.Lazy ) But my ghci does not show Chunk and Empty data constructors. > B.pack [99,97,110] "can" Did Haskell developers change the way the values of ByteString are printed? 回答1: Looks like Duncan added hand-written Show instance for lazy ByteString somewhere between 0.9.2.1 and 0.10.0.1 . See http://hackage.haskell.org/packages/archive/bytestring/0.10.2.0/doc/html/src/Data

Frequency of characters

℡╲_俬逩灬. 提交于 2019-12-21 16:20:01
问题 I am trying to find frequency of characters in file using Haskell . I want to be able to handle files ~500MB size. What I've tried till now It does the job but is a bit slow as it parses the file 256 times calculateFrequency :: L.ByteString -> [(Word8, Int64)] calculateFrequency f = foldl (\acc x -> (x, L.count x f):acc) [] [255, 254.. 0] I have also tried using Data.Map but the program runs out of memory (in ghc interpreter). import qualified Data.ByteString.Lazy as L import qualified Data

Many types of String (ByteString)

帅比萌擦擦* 提交于 2019-12-21 07:04:35
问题 I wish to compress my application's network traffic. According to the (latest?) "Haskell Popularity Rankings", zlib seems to be a pretty popular solution. zlib's interface uses ByteString s: compress :: ByteString -> ByteString decompress :: ByteString -> ByteString I am using regular String s, which are also the data types used by read , show , and Network.Socket : sendTo :: Socket -> String -> SockAddr -> IO Int recvFrom :: Socket -> Int -> IO (String, Int, SockAddr) So to compress my

How to convert a Integer to a ByteString in Haskell

假装没事ソ 提交于 2019-12-18 12:17:11
问题 We'd like to serialize data in a specific binary format. We use Data.ByteString s internally. So, the question is: How to convert the different data types we use to a ByteString . For String we have no problem, we can use encodeLazyByteString UTF8 "string" . But we'd also like to convert Integer s to ByteString s (big-endian). Does anyone know how to do that and/or has any good tips using Haskell and binary formats? Thanks! 回答1: A perfect job for Data.Binary: Prelude> :m + Data.Binary Prelude