@@ -29,15 +29,20 @@ module Truncate.Internal where
29
29
------------------------------------------------------------------------
30
30
import Data.Bits
31
31
import Data.Word
32
- import Data.Char (intToDigit )
33
- import Numeric (showIntAtBase )
32
+ import Data.Char ( digitToInt
33
+ , intToDigit
34
+ )
35
+ import Numeric ( readInt
36
+ , readHex
37
+ , showIntAtBase
38
+ )
34
39
35
40
36
41
------------------------------------------------------------------------
37
42
-- | A wrapper around 'Word32' and 'Word64' types
38
43
data WordF = WordF32 Word32
39
- | WordF64 Word64
40
- deriving (Eq , Show )
44
+ | WordF64 Word64
45
+ deriving (Eq , Show )
41
46
42
47
43
48
------------------------------------------------------------------------
@@ -69,6 +74,9 @@ dropBits n = (flip shiftL) n . (flip shiftR) n
69
74
-----------------------------------------------------------------------
70
75
-- Functions for handling binary representations
71
76
77
+ binToWord :: Num a => String -> a
78
+ binToWord b = fst $ head $ readInt 2 (`elem` " 01" ) digitToInt b
79
+
72
80
wordToBin :: (Show a , Integral a ) => a -> String
73
81
wordToBin b = showIntAtBase 2 intToDigit b " "
74
82
@@ -78,6 +86,9 @@ validBin = all (`elem` "01")
78
86
-----------------------------------------------------------------------
79
87
-- Functions for handling hexadecimal representations
80
88
89
+ hexToWord :: (Num a , Eq a ) => String -> a
90
+ hexToWord s = fst (head (readHex s))
91
+
81
92
wordToHex :: (Show a , Integral a ) => a -> String
82
93
wordToHex b = showIntAtBase 16 intToDigit b " "
83
94
0 commit comments