[Haskell-cafe] Conversion from string to array

John Ky newhoggy at gmail.com
Sat Jun 6 02:13:41 EDT 2009


Hi Haskell Cafe,

I'm trying to send stuff over UDP.  To do that, I've written a test program
that sends strings across.  That was fine, but I wanted to send binary data
too.  So I tried that only to find I am having difficulty putting together
some binary data.  For examples take the fromHex function in the following
code.
It is supposed to convert from a Hexadecimal string to a list of bytes, but
I am having trouble coercing the integer types to the size I want.

Is this the right way to do it?

Cheers,

-John

>  import Data.Bits
>  import Data.Char
>  import Data.Word
>  import System.Environment
>  import XStream.Tsmr.Client

>  data CmdLineOptions = CmdLineOptions
>     { optionHelp :: Bool
>     , optionVersion :: Bool
>     , optionPort :: String
>     , optionMessage :: String
>     , optionInvalids :: [String]
>     }
>     deriving (Eq, Show)

>  initCmdLineOptions = CmdLineOptions
>     { optionHelp = False
>     , optionVersion = False
>     , optionPort = "1234"
>     , optionMessage = ""
>     }

>  parseArgs :: [String] -> CmdLineOptions
>  parseArgs [] = initCmdLineOptions
>  parseArgs ("--port":port:xs) = (parseArgs xs) { optionPort = port }
>  parseArgs ("--help":xs) = (parseArgs xs) { optionHelp = True }
>  parseArgs ("--version":xs) = (parseArgs xs) { optionVersion = True }
>  parseArgs (('-':opt):xs) = let option = (parseArgs xs) in
>     option { optionInvalids = ('-':opt):optionInvalids option }
>  parseArgs (message:xs) = (parseArgs xs) { optionMessage = message }

>  printUsage = do
>     putStrLn "Usage: udp-server.lhs [options] <message>"
>     putStrLn ""
>     putStrLn "Options:"
>     putStrLn "  --help      Get help information."
>     putStrLn "  --vesion    Get version information."
>     putStrLn "  --port n    The port number to listen on."
>     putStrLn ""
>     putStrLn "Message:"
>     putStrLn "  The message to send."
>     putStrLn ""

>  printVersion = do
>     putStrLn "Version."

>  fromHex :: String -> [Word8]
>  fromHex [] = []
>  fromHex (u:l:xs) = (hexU .|. hexL):fromHex xs
>     where
>        hexU = (fromInteger $ hexValue u) :: Word8
>        hexL = (fromInteger $ hexValue l) :: Int
>        hexValue c
>           | '0' <= c && c <= '9' = ord c - ord '0'
>           | 'a' <= c && c <= 'z' = ord c - ord 'a' + 10


>  run port message = do
>     h <- openlog "localhost" port "udp-client.lhs"
>     syslog h (fromHex message)

>  main = do
>     args <- getArgs
>     let options = parseArgs args
>     let port = optionPort options
>     let message = optionMessage options
>     if optionHelp options
>        then printUsage
>        else if optionVersion options
>           then printVersion
>           else do
>              putStrLn ("Starting UDP listener on port: " ++ port)
>              run port message
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090606/1b2f3cb6/attachment.html


More information about the Haskell-Cafe mailing list