From: ludo@gnu.org (Ludovic Courtès)
To: guile-user@gnu.org
Subject: Re: I'm looking for a method of converting a string's character encoding
Date: Fri, 04 May 2012 00:31:09 +0200 [thread overview]
Message-ID: <87ipgcq4lu.fsf@gnu.org> (raw)
In-Reply-To: CAK93xhpSbFQM-FuPAhQVKJgtZHZKO7b2yO--ciNZPenQA_3xkg@mail.gmail.com
Hi,
Sunjoong Lee <sunjoong@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’.
prev parent reply other threads:[~2012-05-03 22:31 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-04-27 21:13 I'm looking for a method of converting a string's character encoding Sunjoong Lee
2012-04-28 1:40 ` Sunjoong Lee
2012-04-28 16:38 ` Sunjoong Lee
2012-04-28 17:33 ` Thien-Thi Nguyen
2012-04-28 18:29 ` Daniel Krueger
2012-04-28 19:54 ` Thien-Thi Nguyen
2012-04-28 20:55 ` Eli Zaretskii
2012-04-28 22:42 ` Sunjoong Lee
2012-04-29 0:25 ` Sunjoong Lee
2012-04-30 10:18 ` Daniel Krueger
2012-04-30 12:21 ` Eli Zaretskii
2012-05-03 22:34 ` Ludovic Courtès
2012-05-02 3:57 ` Daniel Hartwig
2012-05-03 5:14 ` Sunjoong Lee
2012-05-03 22:31 ` Ludovic Courtès [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87ipgcq4lu.fsf@gnu.org \
--to=ludo@gnu.org \
--cc=guile-user@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).