本帖最后由 tryhi 于 2016-4-23 16:28 编辑
今天群友在问关于UTF-8格式的文本如何读取问题,自己写了几条相关函数,可能日后有可能用到,虽然暂时还不是非常完善
注:将Unicode转换为ASCII编码需要加载"简体汉字unicode转GBK.lsp"常量的支持
10进制转二进制
2进制转10进制
16进制转10进制
10进制转16进制
UTF-8编码转Unicode
UTF-8编码转GKB编码- ;;10进制转二进制 BY:大海
- ;;返回一个2进制字符串
- ;;例子:(try-10-to-2 8);-->"1000"
- (defun try-10-to-2(n / m x)
- (setq x "")
- (while (/= 0 n)
- (setq
- m(rem n 2)
- n(/ n 2)
- x(strcat (itoa m)x)
- )
- )
- (if (= "" x)"0"x)
- )
- ;;2进制转10进制 BY:大海
- ;;返回一个整数型的值
- ;;例子:(try-2-to-10 "1000");-->8
- (defun try-2-to-10 (n / f i j m)
- (setq i (strlen n) m 0 j 0)
- (repeat i
- (setq
- f(substr n i 1)
- m(+ m (* (expt 2 j) (atoi f)))
- i(1- i)
- j(1+ j)
- )
- )
- m
- )
- ;;16进制转10进制 BY:大海
- ;;返回一个整数型的值
- ;;例子:(try-16-to-10 "1F");-->31
- (defun try-16-to-10 (n / 16-10 f i ii j m)
- (setq i 0 j (strlen n)m 0
- 16-10 '(("0"0)("1"1)("2"2)("3"3)("4"4)("5"5)("6"6)("7"7)("8"8)("9"9)("A"10)("B"11)("C"12)("D"13)("E"14)("F"15))
- )
- (repeat j
- (setq
- f(substr n j 1)
- ii(cadr(assoc f 16-10))
- m(+ m (* (expt 16 i) ii))
- i(1+ i)
- j(1- j)
- )
- )
- m
- )
- ;;10进制转16进制 BY:大海
- ;;返回一个16进制字符串
- ;;例子:(try-10-to-16 31);-->"1F"
- (defun try-10-to-16(n / 10-16 m n16x x)
- (setq x "" 10-16 '((0"0")(1"1")(2"2")(3"3")(4"4")(5"5")(6"6")(7"7")(8"8")(9"9")(10"A")(11"B")(12"C")(13"D")(14"E")(15"F")))
- (while (/= 0 n)
- (setq
- m(rem n 16)
- n(/ n 16)
- n16x(cadr(assoc m 10-16))
- x(strcat n16x x)
- )
- )
- (if (= x "")"0"x)
- )
- ;;UTF-8编码转Unicode BY:大海
- ;;(try-UTF8->Unicode '(233 155 133));-->(209 197)雅字UTF编码跟Unicode编码
- (defun try-UTF8->Unicode (lst / a1 a2 a3 b c1 c2)
- (setq a1(substr (bu0 8(try-10-to-2 (car lst)))5)
- a2(substr (bu0 8(try-10-to-2 (cadr lst)))3)
- a3(substr (bu0 8(try-10-to-2 (caddr lst)))3)
- b(strcat a1 a2 a3)
- c1(substr b 1 8)
- c2(substr b 9))
- (list (try-2-to-10 c1)(try-2-to-10 c2))
- )
- ;;UTF-8编码转GKB编码 BY:大海
- ;;参数:由3个字节的十进制编码
- ;;例子:"雅"字的UTF8编码为233 155 133,GKB编码为209 197
- ;;(vl-list->string(try-UTF8->GKB '(233 155 133)));-->"雅"
- (defun try-UTF8->GKB(lst / gbk h1 h2 nic nic-16 nicod)
- (setq nic(try-UTF8->Unicode lst))
- (setq nic-16(mapcar '(lambda(x)(bu0 2(try-10-to-16 x)))nic))
- (setq nicod(strcat (car nic-16)(cadr nic-16)))
- (setq gbk(cadr(assoc nicod *u2k*)))
- (setq h1(try-16-to-10(substr gbk 1 2)))
- (setq h2(try-16-to-10(substr gbk 3 2)))
- (list h1 h2)
- )
- ;;补零
- (defun bu0 (n0 num / _000)
- (setq _000 "")
- (repeat(- n0 (strlen num))(setq _000(strcat "0" _000)))
- (strcat _000 num)
- )
|