Last active
December 12, 2015 07:19
-
-
Save jhickner/4735920 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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