| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Text.Hex
- encodeHex :: ByteString -> Text
- decodeHex :: Text -> Maybe ByteString
- lazilyEncodeHex :: LazyByteString -> LazyText
- type Text = Text
- type LazyText = Text
- type ByteString = ByteString
- type LazyByteString = ByteString
- lazyText :: Text -> LazyText
- strictText :: LazyText -> Text
- lazyByteString :: ByteString -> LazyByteString
- strictByteString :: LazyByteString -> ByteString
Encoding and decoding
encodeHex :: ByteString -> Text Source #
Encodes a byte string as hexidecimal number represented in text. Each byte of the input is converted into two characters in the resulting text.
>>>(encodeHex . ByteString.singleton) 192"c0"
>>>(encodeHex . ByteString.singleton) 168"a8"
>>>(encodeHex . ByteString.pack) [192, 168, 1, 2]"c0a80102"
Text produced by encodeHex can be converted back to a
ByteString using decodeHex.
The lazy variant of encodeHex is lazilyEncodeHex.
decodeHex :: Text -> Maybe ByteString Source #
Decodes hexidecimal text as a byte string. If the text contains
an even number of characters and consists only of the digits 0
through 9 and letters a through f, then the result is a
Just value.
>>>(fmap ByteString.unpack . decodeHex . Text.pack) "c0a80102"Just [192,168,1,2]
If the text contains an odd number of characters, decoding fails
and produces Nothing.
>>>(fmap ByteString.unpack . decodeHex . Text.pack) "c0a8010"Nothing
If the text contains non-hexidecimal characters, decoding fails
and produces Nothing.
>>>(fmap ByteString.unpack . decodeHex . Text.pack) "x0a80102"Nothing
The letters may be in either upper or lower case. This next example therefore gives the same result as the first one above:
>>>(fmap ByteString.unpack . decodeHex . Text.pack) "C0A80102"Just [192,168,1,2]
lazilyEncodeHex :: LazyByteString -> LazyText Source #
lazilyEncodeHex is the lazy variant of encodeHex.
With laziness, it is possible to encode byte strings of infinite length:
>>>(LazyText.take 8 . lazilyEncodeHex . LazyByteString.pack . cycle) [1, 2, 3]"01020301"
Types
type ByteString = ByteString Source #
Strict byte string
type LazyByteString = ByteString Source #
Lazy byte string
Type conversions
strictText :: LazyText -> Text Source #