HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Hex to ASCII conversion in Haskell

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
conversionhexasciihaskell

Problem

I wrote a simple program in Haskell which takes Hex input from stdin, converts the input to ascii and writes it to stdout. An example use:

$ echo -n '48656c6c6f2c2043522e534521' | unhex
Hello, CR.SE!


And here the source code:

import qualified Data.ByteString as B
import Data.Bits
import Data.Word

unhex :: [Word8] -> [Word8]
unhex [] = []
unhex (x:[]) = error "invalid input length"
unhex (x:y:xs) = [(shiftL (hex2val x) 4) .|. (hex2val y)] ++ unhex xs
    where hex2val x =  hex2val' $ (x .|. 0x20) -- lowercase char
          hex2val' x 
        | x >= 48 && x = 97 && x < 97 +  6 = 10 + x - 97 -- 97 is 'a' in ascii
        | otherwise              = error "invalid hex input"

main = do
    input <- B.getContents
    B.putStrLn $ B.pack $ unhex $ B.unpack input


This is one of my first Haskell programs I wrote, would be interesting to get some feedback on how to accomplish this task more the "Haskell" way. I'd like to know if this kind of error reporting is appropriate.

Solution

Calling error is fine for a small program like this.

For more advanced error handling see the Failure page on HaskellWiki.

Some comments about your code:

unhex (x:[]) = error "invalid input length"


x is not used here, use this pattern instead: (_:[])

unhex (x:y:xs) = [(shiftL (hex2val x) 4) .|. (hex2val y)] ++ unhex xs


[x] ++ xs is usually written x : xs

hex2val' $ (x .|. 0x20)


$ is useless here.

Code Snippets

unhex (x:[]) = error "invalid input length"
unhex (x:y:xs) = [(shiftL (hex2val x) 4) .|. (hex2val y)] ++ unhex xs
hex2val' $ (x .|. 0x20)

Context

StackExchange Code Review Q#30076, answer score: 2

Revisions (0)

No revisions yet.