Encoding 'utf-16' is not consistent when convert lisp string from/to C string -
i find when use 'utf-16' encoding convert lisp string c string cffi, actual encoding used 'utf-16le'. but, when convert c string lisp string, actual encoding used 'utf-16be'. since i'm not familiar 'babel' yet (which provides encoding facility 'cffi'), i'm not sure whether that's bug.
(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" the `convtest' convert lisp string c string lisp string, `to-c', `from-c' encoding. output garbage string same. test see if use 'utf-16' `to-c' , `from-c' @ same time, conversion failed.
here encoding to-c assumes little endian (le) default. from-c has big-endian default (be).
the platform (x86) little endian. utf-16 prefers big endian or takes information byte-order mark.
this depends on platform running on? platforms seem have different defaults.
best source code, why encodings chosen. may ask on cffi mailing list encoding choices , how depend on platform, if @ all.
Comments
Post a Comment