我发现当使用'utf-16'作为编码将lisp字符串转换为cffi的C字符串时,实际使用的编码是'utf-16le'。但是,当将 C 字符串转换回 lisp 字符串时,使用的实际编码是 'utf-16be'。由于我还不熟悉“babel”(它为“cffi”提供了编码工具),我不确定这是否是一个错误。
(defun convtest (str to-c from-c)
(multiple-value-bind (ptr size)
(cffi:foreign-string-alloc str :encoding to-c)
(declare (ignore size))
(prog1
(cffi:foreign-string-to-lisp ptr :encoding from-c)
(cffi:foreign-string-free ptr))))
(convtest "hello" :utf-16 :utf-16) ;=> garbage string
(convtest "hello" :utf-16 :utf-16le) ;=> "hello"
(convtest "hello" :utf-16 :utf-16be) ;=> garbage string
(convtest "hello" :utf-16le :utf-16be) ;=> garbage string
(convtest "hello" :utf-16le :utf-16le) ;=> "hello"
`convtest' 将 lisp 字符串转换为 C 字符串,然后再转换回 lisp 字符串,使用 `to-c'、`from-c' 作为编码。所有输出的垃圾字符串都是一样的。从测试中我们看到,如果我们同时使用'utf-16'作为'to-c'和'from-c',转换失败。