{-# LANGUAGE OverloadedStrings #-}
module Data.MIME.Internal
(
hexEncode
, parseHex
) where
import Data.Bits ((.&.), shiftR)
import Data.Word (Word8)
import qualified Data.ByteString as B
parseHex :: Word8 -> Maybe Word8
parseHex c = do
let
c' = if c >= 0x61 && c <= 0x7a then c - 0x20 else c
fromIntegral <$> B.findIndex (== c') hexAlphabet
hexAlphabet :: B.ByteString
hexAlphabet = "0123456789ABCDEF"
hexEncode :: Word8 -> (Word8, Word8)
hexEncode c =
let
lkup i = B.index hexAlphabet (fromIntegral i)
in
( lkup (c `shiftR` 4)
, lkup (c .&. 0x0f)
)