[Openmcl-devel] encoding/decoding clozure strings to/from UTF-8

Karsten Poeck Karsten.Poeck at gmail.com
Tue Dec 30 11:13:23 PST 2014


On 30.12.14 19:46, R. Matthew Emerson wrote:
>
>> On Dec 30, 2014, at 1:19 PM, Mark Klein <m_klein at MIT.EDU> wrote:
>>
>>
>> I have Clozure 1.10 running on a Mac (OS 10.10.1). I'd like to be able to encode strings to and from UTF-8 (to handle multi-language text in a web application I am developing). Can someone give me some pointers on how I can do that?
>
> http://ccl.clozure.com/docs/ccl.html#encoding-and-decoding-strings
>
> Basically, use (encode-string-to-octets string :external-format :utf-8) to encode a lisp string into a vector of octets containing UTF-8-encoded data, and use (decode-string-from-octets vector :external-format :utf-8) to create a lisp string from a vector of octets containing UTF-8-encoded data.

Since Matthew already answered the original question, another example 
with 1,2 and 3 byte encoding in utf-8 from 
http://en.wikipedia.org/wiki/UTF-8

(map 'vector #'(lambda(byte)(write-to-string byte :base 16)) 
(encode-string-to-octets "$¢€" :external-format :utf-8 
:use-byte-order-mark nil))

#("24" "C2" "A2" "E2" "82" "AC")

To read an utf-8 encoded file use
(defun read-utf-8-file (file)
   (with-open-file (stream file :direction :input :external-format :utf-8)
  .......)

regards

Karsten





More information about the Openmcl-devel mailing list