how to use a literal string with bystring useAsCString

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. Any idea ? Thanks, Thu

2009/2/9 minh thu
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 ?

2009/2/9 minh thu
Anyway, I'd like to get my utf-8 string to C but in ascii (or latin1).
How can do this ?
You can't (in general). If the data just happens to be ascii, then your utf-8 string will BE ascii (there is no way to tell the difference). If it just happens to be in the range of latin-1(all codepoints 0-255), then you have to convert it to latin-1. But otherwise, you are stuck.

2009/2/9 Colin Adams
2009/2/9 minh thu
: Anyway, I'd like to get my utf-8 string to C but in ascii (or latin1).
How can do this ?
You can't (in general). If the data just happens to be ascii, then your utf-8 string will BE ascii (there is no way to tell the difference). If it just happens to be in the range of latin-1(all codepoints 0-255), then you have to convert it to latin-1. But otherwise, you are stuck.
That's what I thought, but in the code attached before, the output of the program is not what is expected, i.e. I have : 0xb7d7e018 0xb7d7e040 Why ? Thu

2009/2/9 minh thu
2009/2/9 Colin Adams
: 2009/2/9 minh thu
: Anyway, I'd like to get my utf-8 string to C but in ascii (or latin1).
How can do this ?
You can't (in general). If the data just happens to be ascii, then your utf-8 string will BE ascii (there is no way to tell the difference). If it just happens to be in the range of latin-1(all codepoints 0-255), then you have to convert it to latin-1. But otherwise, you are stuck.
That's what I thought, but in the code attached before, the output of the program is not what is expected, i.e. I have : 0xb7d7e018 0xb7d7e040 Why ?
Thu
Sorry, in both case, it's a mistake on my side. In particular, in the attached code, I call 'print' instead of 'c_print'. Thu
participants (2)
-
Colin Adams
-
minh thu