-- | Dump a ByteString as a String of hexidecimal characters.
asHex :: B.ByteString -> String
asHex str = foldr hex "" $ B.unpack str where
  hex :: Word8 -> String -> String
  hex c rest = hexNybble (c `shiftR` 4) : hexNybble (c .&. 0xF) : rest
  hexNybble :: Word8 -> Char
  hexNybble c = ("0123456789abcdef" !! fromIntegral c)

-- | Parse a string of hex into a ByteString.
asHex :: B.ByteString -> String
asHex str = foldr hex "" $ B.unpack str where
  hex :: Word8 -> String -> String
  hex c rest = hexNybble (c `shiftR` 4) : hexNybble (c .&. 0xF) : rest
  hexNybble :: Word8 -> Char
  hexNybble c = ("0123456789abcdef" !! fromIntegral c)

-- | Parse a string of hex into a ByteString.
asHex str = foldr hex "" $ B.unpack str where
  hex :: Word8 -> String -> String
  hex c rest = hexNybble (c `shiftR` 4) : hexNybble (c .&. 0xF) : rest
  hexNybble :: Word8 -> Char
  hexNybble c = ("0123456789abcdef" !! fromIntegral c)

-- | Parse a string of hex into a ByteString.