bytestring

How to convert a Integer to a ByteString in Haskell

半腔热情 提交于 2019-11-30 06:44:51
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! A perfect job for Data.Binary: Prelude> :m + Data.Binary Prelude Data.Binary> encode (pi :: Double) Chunk "\SOH\SOH\NUL\NUL\NUL\NUL\NUL\NUL\NUL\a\CAN-DT\251!\EM\255\255\255

Haskell Bytestrings: How to pattern match?

主宰稳场 提交于 2019-11-30 03:21:38
I'm a Haskell newbie, and having a bit of trouble figuring out how to pattern match a ByteString . The [Char] version of my function looks like: dropAB :: String -> String dropAB [] = [] dropAB (x:[]) = x:[] dropAB (x:y:xs) = if x=='a' && y=='b' then dropAB xs else x:(dropAB $ y:xs) As expected, this filters out all occurrences of "ab" from a string. However, I have problems trying to apply this to a ByteString . The naive version dropR :: BS.ByteString -> BS.ByteString dropR [] = [] dropR (x:[]) = [x] <...> yields Couldn't match expected type `BS.ByteString' against inferred type `[a]' In the

Python3 subprocess output

僤鯓⒐⒋嵵緔 提交于 2019-11-29 02:09:05
问题 I'm wanting to run the Linux word count utility wc to determine the number of lines currently in the /var/log/syslog, so I can detect that it's growing. I've tried various test, and while I get the results back from wc, it includes both the line count as well as the command (e.g., var/log/syslog). So it's returning: 1338 /var/log/syslog But I only want the line count, so I want to strip off the /var/log/syslog portion, and just keep 1338. I have tried converting it to string from bytestring,

Using Haskell to output a UTF-8-encoded ByteString

女生的网名这么多〃 提交于 2019-11-29 01:06:50
I'm going out of my mind trying to simply output UTF-8-encoded data to the console. I've managed to accomplish this using String , but now I'd like to do the same with ByteString . Is there a nice and fast way to do this? This is what I've got so far, and it's not working: import Prelude hiding (putStr) import Data.ByteString.Char8 (putStr, pack) main :: IO () main = putStr $ pack "čušpajž日本語" It prints out uapaj~�,� , ugh. I'd like an answer for the newest GHC 6.12.1 best, although I'd like to hear answers for previous versions as well. Thanks! Update: Simply reading and outputting the same

Haskell Lazy ByteString + read/write progress function

别来无恙 提交于 2019-11-28 17:17:19
I am learing Haskell Lazy IO. I am looking for an elegant way to copy a large file (8Gb) while printing copy progress to console. Consider the following simple program that copies a file silently. module Main where import System import qualified Data.ByteString.Lazy as B main = do [from, to] <- getArgs body <- B.readFile from B.writeFile to body Imgine there is a callback function you want to use for reporting: onReadBytes :: Integer -> IO () onReadBytes count = putStrLn $ "Bytes read: " ++ (show count) QUESTION: how to weave onReadBytes function into Lazy ByteString so it will be called back

Using Haskell to output a UTF-8-encoded ByteString

微笑、不失礼 提交于 2019-11-27 21:37:04
问题 I'm going out of my mind trying to simply output UTF-8-encoded data to the console. I've managed to accomplish this using String , but now I'd like to do the same with ByteString . Is there a nice and fast way to do this? This is what I've got so far, and it's not working: import Prelude hiding (putStr) import Data.ByteString.Char8 (putStr, pack) main :: IO () main = putStr $ pack "čušpajž日本語" It prints out uapaj~�,� , ugh. I'd like an answer for the newest GHC 6.12.1 best, although I'd like

What is a Python bytestring?

荒凉一梦 提交于 2019-11-27 01:40:51
What's a Python bytestring? All I can find are topics on how to encode to bytestring or decode to ascii or utf-8 . I'm trying to understand how it works under the hood. In a normal ASCII string, it's an array or list of characters, and each character represents an ASCII value from 0-255, so that's how you know what character is represented by the number. In Unicode, it's the 8- or 16-byte representation for the character that tells you what character it is. So what is a bytestring? How does Python know which characters to represent as what? How does it work under the hood? Since you can print

What is a Python bytestring?

折月煮酒 提交于 2019-11-26 09:39:01
问题 What\'s a Python bytestring? All I can find are topics on how to encode to bytestring or decode to ascii or utf-8 . I\'m trying to understand how it works under the hood. In a normal ASCII string, it\'s an array or list of characters, and each character represents an ASCII value from 0-255, so that\'s how you know what character is represented by the number. In Unicode, it\'s the 8- or 16-byte representation for the character that tells you what character it is. So what is a bytestring? How