1 -- This file is part of htalkat
2 -- Copyright (C) 2021 Martin Bays <mbays@sdf.org>
4 -- This program is free software: you can redistribute it and/or modify
5 -- it under the terms of version 3 of the GNU General Public License as
6 -- published by the Free Software Foundation, or any later version.
8 -- You should have received a copy of the GNU General Public License
9 -- along with this program. If not, see http://www.gnu.org/licenses/.
13 module HexString
where
15 import Data
.ByteString
(ByteString
, pack
, unpack
)
17 parseHexString
:: String -> Maybe ByteString
18 parseHexString s
= pack
<$> parseHexString
' s
20 parseHexString
' (a
:b
:c
) = do
23 (16 * ha
+ hb
:) <$> parseHexString
' c
48 parseHexString
' [] = Just
[]
49 parseHexString
' _
= Nothing
51 showHexString
:: ByteString
-> String
52 showHexString
= concatMap showHexWord8
. unpack
53 where showHexWord8 w
=
54 let (a
,b
) = quotRem w
16
55 hex
= ("0123456789abcdef" !!) . fromIntegral