Re: [Haskell-cafe] Conversion from string to array

2009-06-05 Thread Neil Davies

Look at Data.Binary (binary package)

It will marshall and unmarshall data types for you. If you don't like  
its binary encoding you can dive in there and use the same principles


Cheers

Neil

On 6 Jun 2009, at 07:13, John Ky wrote:


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] "
> putStrLn ""
> putStrLn "Options:"
> putStrLn "  --help  Get help information."
> putStrLn "  --vesionGet version information."
> putStrLn "  --port nThe 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





___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Conversion from string to array

2009-06-05 Thread John Ky
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] "
> putStrLn ""
> putStrLn "Options:"
> putStrLn "  --help  Get help information."
> putStrLn "  --vesionGet version information."
> putStrLn "  --port nThe 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
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe