Skip to content

Instantly share code, notes, and snippets.

@jhickner
Last active December 12, 2015 07:19
Show Gist options
  • Save jhickner/4735920 to your computer and use it in GitHub Desktop.
Save jhickner/4735920 to your computer and use it in GitHub Desktop.
import Data.Binary.Put (runPut)
import Data.Binary.Get (runGet)
import Data.Binary.Bits.Put
import Data.Binary.Bits.Get
-- helper function to create a bytestring from a list of [Int]
toByteString = runPut . runBitPut . mapM_ (putBool . toBool)
where
toBool n = n /= 0 || False
-- test packet
packet = toByteString [1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1]
-- the parser. reads the first 4 bits as a length,
-- then length more bits as a 16-bit word
parse = runGet . runBitGet $ do
len <- getWord8 4
getWord16be (fromIntegral len)
-- > parse packet
-- > 151
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment