guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: I'm looking for a method of converting a string's character encoding


From: Ludovic Courtès
Subject: Re: I'm looking for a method of converting a string's character encoding
Date: Fri, 04 May 2012 00:31:09 +0200
User-agent: Gnus/5.110018 (No Gnus v0.18) Emacs/24.0.93 (gnu/linux)

Hi,

Sunjoong Lee <address@hidden> skribis:

> I'm looking for a method of converting a string's character encoding from a
> certain codeset to utf-8. I know the string of Guile uses utf-8 and (read
> (open-bytevector-input-port (string->utf8 "hello"))) returns "hello" . But
> what if the string "hello" be encoded not utf-8 and you want to get utf-8
> converted string? What I want is like iconv.

Ports in Guile are both binary and textual.  This allows for things like:

  scheme@(guile-user)> (use-modules (rnrs io ports))
  scheme@(guile-user)> (define (string->enc s e)
                         (let ((p (with-fluids ((%default-port-encoding e))
                                    (open-input-string s))))
                           (get-bytevector-all p)))
  scheme@(guile-user)> (string->enc "hello" "UTF-16BE")
  $1 = #vu8(0 104 0 101 0 108 0 108 0 111)
  scheme@(guile-user)> (string->enc "hello" "ISO-8859-3")
  $2 = #vu8(104 101 108 108 111)
  scheme@(guile-user)> (use-modules (rnrs bytevectors))
  scheme@(guile-user)> (utf16->string $1)
  $3 = "hello"

You may also want to look at ‘string->pointer’ in (system foreign).

Does it answer your question?

Thanks,
Ludo’.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]