Cast from and to CChar

John Meacham john at repetae.net
Mon Oct 27 13:49:10 EST 2003


On Mon, Oct 27, 2003 at 08:47:41PM +0100, Christian Buschmann wrote:
> Hi!
> I've got following problem. If I enter in ghci following line:
>   Prelude Foreign.C> castCCharToChar $ castCharToCChar 'ü'
> I would expect that this returns 'ü', but it returns '\252'. Is this the 
> correct behaviour? Or am I doing something wrong? Or are there any 
> problems with language specific characters and CChar?

The problem is a CChar is (most likely) 8 bits while a Haskell Char is a
32 bit unicode value. the correct thing to do to talk unicode values
with C code can depend on what you are trying to do, if you know your
system is utf8 (many are) and don't mind being somewhat unportable, then
the easist thing to do is just hard code that in
http://repetae.net/john/computer/haskell/UTF8.hs will do it. (code
stolen from someone else)

if you want to use the proper locale  settings, then things get
trickier but this should do it for many apps but requires the ffi
http://repetae.net/john/computer/haskell/CWString.hsc

otherwise, you may need to write your own ffi code which uses 'iconv' to
do the proper character set conversion. This is a well known deficiency
in the haskell libraries at the moment...

        John

-- 
---------------------------------------------------------------------------
John Meacham - California Institute of Technology, Alum. - john at foo.net
---------------------------------------------------------------------------


More information about the Haskell-Cafe mailing list