你好
云你能帮帮我吗?
这个代码怎么了?
- [color="green"];Reference, post#138
- ;https://stackoverflow.com/questions/643694/what-is-the-difference-between-utf-8-and-unicode[/color]
- (defun [color="blue"]UTF8->unicode[/color] ( l / ls 8b d2 foo) ; encode UTF-8 to unicode
- [color="green"];;;hanhphuc 17.04.2018 [/color]
- (setq 8b '((s) (while (< (strlen s) (setq s (strcat "0" s))) s)
- d2 '((str) ;split string to two list
- (if (> (strlen str) 0)
- (cons (substr str 1 (d2 (setq str (substr str 9 ))))
- )
- )
- foo '(($ / pos i) ; base2 to decimal
- (setq i 0)
- (+ (cond ((while (and (> (strlen $) 0) (setq pos (vl-string-search "1" $)))
- (setq $ (substr $ (+ 2 pos))
- i (+ i (expt 2 (strlen $)))
- )
- )
- )
- (0)
- )
- (atoi $)
- )
- )
- ls (mapcar ''((x / $)
- (setq $ (LM:dec->base (foo x) 16))
- (if
- (= (strlen $) 1)
- (strcat "0" $)
- $
- )
- )
- (d2
- (apply 'strcat
- (mapcar ''((a x) (substr (8b a) (- 9 x) x))
- l
- (cdr (assoc (length l) '((1 . (7)) (2 . (5 6)) (3 . (4 6 6)) (4 . (3 6 6 6)))))
- )
- )
- )
- )
- )
- (apply 'strcat
- (vl-list* "\\U"
- (if (> (length ls) 1)
- "+"
- "+00")
- ls
- )
- )
- )
- (defun [color="blue"]U8:bytes[/color] (l / x ls)
- [color="green"];hanhphuc 17.04.2018[/color]
- ;UTF-8 split the bytes
- (setq x (car l))
- (if l
- (cons (vl-remove nil (cond ((<= 0 x 191)
- (setq ls (list x)
- l (cdr l)
- )
- ls
- )
- ((<= 192 x 223)
- (setq ls (list x (cadr l))
- l (cddr l)
- )
- ls
- )
- ((<= 224 x 239)
- (setq ls (list x (cadr l) (caddr l))
- l (cdddr l)
- )
- ls
- )
- ((<= 240 x 247)
- (setq ls (list x (cadr l) (caddr l) (cadddr l))
- l (cddddr l)
- )
- ls
- )
- )
- )
- (U8:bytes l)
- )
- )
- )
所以换成这个
- (setq ret [b][color="purple"]"Lee Mac & Marko Ribar\r\nHappy Birthday\r\nç¥ä½*们生日快ä¹\r\n幸ç¦\r\nChúc mừng sinh nháº*t\r\n"[/color][/b]
- )
愉快地编码
p/s:使用read char读取unicode文件 |