Hi,

Sunjoong Lee <sunjo...@gmail.com> 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