Safe Haskell | None |
---|---|
Language | Haskell2010 |
Peek and poke functions for network byte order.
Synopsis
- type Buffer = Ptr Word8
- type Offset = Int
- poke8 :: Word8 -> Buffer -> Offset -> IO ()
- poke16 :: Word16 -> Buffer -> Offset -> IO ()
- poke24 :: Word32 -> Buffer -> Offset -> IO ()
- poke32 :: Word32 -> Buffer -> Offset -> IO ()
- poke64 :: Word64 -> Buffer -> Offset -> IO ()
- peek8 :: Buffer -> Offset -> IO Word8
- peek16 :: Buffer -> Offset -> IO Word16
- peek24 :: Buffer -> Offset -> IO Word32
- peek32 :: Buffer -> Offset -> IO Word32
- peek64 :: Buffer -> Offset -> IO Word64
- bytestring8 :: Word8 -> ByteString
- bytestring16 :: Word16 -> ByteString
- bytestring32 :: Word32 -> ByteString
- bytestring64 :: Word64 -> ByteString
- word8 :: ByteString -> Word8
- word16 :: ByteString -> Word16
- word32 :: ByteString -> Word32
- word64 :: ByteString -> Word64
- unsafeWithByteString :: ByteString -> (Buffer -> Offset -> IO a) -> IO a
Types
Poking
poke8 :: Word8 -> Buffer -> Offset -> IO () Source #
>>>
let buf = pack [1,2,3,4]
>>>
unsafeWithByteString buf (poke8 0)
>>>
unpack buf
[0,2,3,4]
poke16 :: Word16 -> Buffer -> Offset -> IO () Source #
>>>
let buf = pack [1,2,3,4]
>>>
unsafeWithByteString buf (poke16 (7*256 + 8))
>>>
unpack buf
[7,8,3,4]
poke24 :: Word32 -> Buffer -> Offset -> IO () Source #
>>>
let buf = pack [1,2,3,4]
>>>
unsafeWithByteString buf (poke24 (6*65536 + 7*256 + 8))
>>>
unpack buf
[6,7,8,4]
poke32 :: Word32 -> Buffer -> Offset -> IO () Source #
>>>
let buf = pack [1,2,3,4]
>>>
unsafeWithByteString buf (poke32 (6*65536 + 7*256 + 8))
>>>
unpack buf
[0,6,7,8]
poke64 :: Word64 -> Buffer -> Offset -> IO () Source #
>>>
let buf = pack [1,2,3,4,5,6,7,8]
>>>
unsafeWithByteString buf (poke64 (6*65536 + 7*256 + 8))
>>>
unpack buf
[0,0,0,0,0,6,7,8]
Peeking
peek8 :: Buffer -> Offset -> IO Word8 Source #
>>>
let buf = pack [1,2,3,4]
>>>
unsafeWithByteString buf peek8
1
peek16 :: Buffer -> Offset -> IO Word16 Source #
>>>
let buf = pack [1,2,3,4]
>>>
unsafeWithByteString buf peek16
258
peek24 :: Buffer -> Offset -> IO Word32 Source #
>>>
let buf = pack [1,2,3,4]
>>>
unsafeWithByteString buf peek24
66051
peek32 :: Buffer -> Offset -> IO Word32 Source #
>>>
let buf = pack [1,2,3,4]
>>>
unsafeWithByteString buf peek32
16909060
peek64 :: Buffer -> Offset -> IO Word64 Source #
>>>
let buf = pack [1,2,3,4,5,6,7,8]
>>>
unsafeWithByteString buf peek64
72623859790382856
From Word to ByteString
bytestring8 :: Word8 -> ByteString Source #
>>>
let w = 5 :: Word8
>>>
unpack $ bytestring8 w
[5]
bytestring16 :: Word16 -> ByteString Source #
>>>
let w = foldl' (\x y -> x * 256 + y) 0 [5,6] :: Word16
>>>
unpack $ bytestring16 w
[5,6]
bytestring32 :: Word32 -> ByteString Source #
>>>
let w = foldl' (\x y -> x * 256 + y) 0 [5,6,7,8] :: Word32
>>>
unpack $ bytestring32 w
[5,6,7,8]
bytestring64 :: Word64 -> ByteString Source #
>>>
let w = foldl' (\x y -> x * 256 + y) 0 [1,2,3,4,5,6,7,8] :: Word64
>>>
unpack $ bytestring64 w
[1,2,3,4,5,6,7,8]
From ByteString to Word
word8 :: ByteString -> Word8 Source #
>>>
let buf = pack [1,2,3,4,5,6,7,8]
>>>
word8 buf
1
word16 :: ByteString -> Word16 Source #
>>>
let buf = pack [1,2,3,4,5,6,7,8]
>>>
word16 buf
258
word32 :: ByteString -> Word32 Source #
>>>
let buf = pack [1,2,3,4,5,6,7,8]
>>>
word32 buf
16909060
word64 :: ByteString -> Word64 Source #
>>>
let buf = pack [1,2,3,4,5,6,7,8]
>>>
word64 buf
72623859790382856
Utilities
unsafeWithByteString :: ByteString -> (Buffer -> Offset -> IO a) -> IO a Source #
Using ByteString
as Buffer
and call the IO
action
of the second argument by passing the start point and the offset
of the ByteString
.
Note that if a ByteString
is created newly, its offset is 0.