Note: This is taken from the Chicken Wiki, where a more recent version could be available.


Allows conversion of multi-byte sequences from one character set (encoding) to another by means of the iconv functionality present in glibc (and thus is probably not very portable).



(use iconv)

; First we obtain a conversion descriptor.  In this case we will convert
; from latin1 to utf-8:

(define *desc* (iconv-open "utf-8" "latin1"))

; Read lines from the input, convert them and write them:

(let loop ((line (read-line)))
  (unless (eof-object? line)
    (display (iconv *desc* line))
    (loop (read-line))))


This egg was created by Alejandro Forero Cuervo.


The iconv egg for Chicken Scheme is in the public domain and may be reproduced or copied without permission from its author. Citation of the source is appreciated.



[procedure] (iconv-open TOCODE FROMCODE)

Allocate a conversion descriptor suitable for converting byte sequences from character encoding FROMCODE (a string) to character encoding TOCODE (a string).

The values permitted for FROMCODE and TOCODE and the supported combinations are system dependent. For the GNU C library, the permitted values are listed by the iconv --list command, and all combinations of the listed values are supported.

The resulting conversion descriptor can be used with iconv any number of times.


[procedure] (iconv CD SRC [INVALID [DSTLEN]])

The argument cd must be a conversion descriptor created using the function iconv-open. iconv converts the multibyte sequence SRC (a string) according to the character sets specified when CD was created.

If an invalid character is found (because of an invalid or incomplete multibyte character sequence), one byte is skipped from the input and the string INVALID, which defaults to "?", is used.

DSTLEN is an integer specifying the initial size for the buffer where the results are stored. The buffer will be dynamically adjusted to hold the entire result.

The resulting string is returned (or #f if the implementation runs out of memory).

Version history

Use of 'foreign-safe-lambda*'. [Kon Lovett]
Links with libiconv when (software-version) is unknown. This is the case of Cygwin, which requires libiconv. This was suggested by Dekai Wu.
Links with libiconv on Mac OS X.
Uses callback to allocate result buffer.
First release.