2009/2/9 minh thu <[email protected]>:
> Hi,
>
> I have a Haskell source file encoded in utf-8.
> Inside that source, I have literal strings that I'd
> like to pass to a C function.
>
> withCString does the job well until I tried to use
> the double-quote character ". I get
> /usr/lib/ghc-6.10.1/ghc: `@: Bad font file format
> (even when using (chr 34) instead).
>
> I didn't understand the reason of this behavior (since
> the double quote is just ascii) but
> tried to use useAsCString but coudn't do it.
> I can ByteString.Char8.pack my string but the problem
> remains. I tried to use IConv but it uses UTF8.ByteString
> and I don't know how to make the conversion so I can
> use useAsCString or withCString.

Bulat asked me a minimal example.
I cannot come with the same behavior with ghc --make
and the attached code.
Maybe it is showed inside ghci.

Anyway, I'd like to get my utf-8 string to C but in ascii (or latin1).

How can do this ?
{-# LANGUAGE ForeignFunctionInterface #-}
module Main where

import Foreign.C.String (CString, withCString)
import Foreign.C.Types  (CInt)
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as C
import qualified Data.ByteString.UTF8 as U
import Codec.Text.IConv (convert)

foreign import ccall "literal.h print" c_print
  :: CString -> IO CInt

-- Can't pack :
-- Expected type: [GHC.Word.Word8]
-- Inferred type: [Char]
--bytestr = B.pack "Pack Me."

str1 = "Hello world !"
bytestr1 = C.pack "Bye !"

-- How to use convert ?
-- Expected type `Data.ByteString.Lazy.Internal.ByteString'
-- Inferred type `C.ByteString'
--bytestr3 = convert "UTF-8" "LATIN1" $ C.pack "Pack"

-- Similar.
--bytestr3 = convert "UTF-8" "LATIN1" $ U.fromString "Pack"

main = do
  withCString str1 print
  B.useAsCString bytestr1 print


#include <stdio.h>

int print (char * msg) { printf ("%s\n", msg); }

int print (char * msg);

_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to