File: Serialize.hs

package info (click to toggle)
haskell-cryptocipher 0.3.5-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 256 kB
  • sloc: haskell: 2,916; ansic: 142; makefile: 3
file content (26 lines) | stat: -rw-r--r-- 713 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
module Number.Serialize
	( i2osp
	, os2ip
	, lengthBytes
	) where

import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import Data.Bits

-- | os2ip converts a byte string into a positive integer
os2ip :: ByteString -> Integer
os2ip = B.foldl' (\a b -> (256 * a) .|. (fromIntegral b)) 0

-- | i2osp converts a positive integer into a byte string
i2osp :: Integer -> ByteString
i2osp m = B.reverse $ B.unfoldr divMod256 m
	where
		divMod256 0 = Nothing
		divMod256 n = Just (fromIntegral a,b) where (b,a) = n `divMod` 256

-- | returns the number of bytes to store an integer with i2osp
lengthBytes :: Integer -> Int
lengthBytes n
	| n < 256   = 1
	| otherwise = 1 + lengthBytes (n `div` 256)