乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 35|回复: 9

[编程交流] 如何读取UTF-8编码?

[复制链接]
mit

13

主题

33

帖子

20

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
65
发表于 2022-7-5 15:12:24 | 显示全部楼层 |阅读模式
大家好
 
我试图编写代码来读取数据。txt文件并填充到Autocad Map中的对象数据表中,但它被显示了????????
 

                               
登录/注册后可看大图

 
云你能帮帮我吗?
 
测验txt文件
尚塔博里。图纸
填充OD_表。lsp
161232bez6bwr0rhdriejn.jpg
回复

使用道具 举报

5

主题

956

帖子

963

银币

初来乍到

Rank: 1

铜币
35
发表于 2022-7-5 15:25:30 | 显示全部楼层
 
不确定,但请尝试一下,如果不是一个好的解决方案,很抱歉
  1. Can't display "[color="red"]ດິນບຸກຄົນ[/color]" ?
  2. [color="green"];;;(setq remark (vk_ReadTextStream "C:/test.txt" "UTF-8"))[/color]
  3. [color="green"];Try alternative way manually copy text from text file then paste[/color]
  4. (setq [b]remark[/b] ([color="blue"]getstring [/color] "\nPaste our text here -> "))
  5. [color="green"];"\U+0E94\U+0EB4\U+0E99\U+0E9A\U+0EB8\U+0E81\U+0E84\U+0EBB\U+0E99"[/color]
  6. [color="green"];or dialog [/color]
  7. (setq [b]remark[/b] (lisped "paste here ") )
回复

使用道具 举报

mit

13

主题

33

帖子

20

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
65
发表于 2022-7-5 15:29:02 | 显示全部楼层
谢谢hanhphuc我会试试的
回复

使用道具 举报

5

主题

956

帖子

963

银币

初来乍到

Rank: 1

铜币
35
发表于 2022-7-5 15:33:02 | 显示全部楼层
一些亚洲字体可以显示正常的打开功能,但它只支持ANSI
如果初始对为FE FF(十六进制)或254 255
 
保存测试。txt作为Unicode
 
  1. (setq f (open path "r"))
  2. (setq ret (read-line f))[color="green"] ;<--test only 1st line[/color]
  3. (if f (close f))

 
测验
  1. (defun foo ( str ) ; read unicode - test version
  2. hanhphuc 17.04.2018
  3. (apply 'strcat
  4. (mapcar
  5.           ''( ( x ) (apply 'strcat (vl-list* (chr 92) "U+" (mapcar ''( (x / $)  (setq $ ( LM:dec->base x 16))
  6.                                           (if (or (< x 10) (=(strlen $)1)) (strcat "0" $) $) )
  7.                                     (reverse x)
  8.                                 )
  9.                    )
  10.                )
  11.                 )
  12.        
  13.         (
  14.          '( ( f ) (f (vl-remove-if
  15.                       '(lambda (x) (vl-some '(lambda (y)
  16.                                                (= x y)
  17.                                                )
  18.                                            '( 254 255 ))
  19.                                       )
  20.                         (vl-string->list str)
  21.                       )
  22.                    )
  23.                         )
  24.          '( ( l ) (if l (cons (list (car l)(cadr l))
  25.                                 (f (cddr l)))
  26.                    )
  27.             )
  28.         )       
  29. )
  30. )
  31. )
  32. ;; Decimal to Base  -  Lee Mac
  33. ;; Converts a decimal number to another base.
  34. ;; n - [int] decimal integer
  35. ;; b - [int] non-zero positive integer base
  36. ;; Returns: [str] Representation of decimal in specified base
  37. (defun LM:dec->base ( n b )
  38.    (if (< n b)
  39.        (chr (+ n (if (< n 10) 48 55)))
  40.        (strcat (LM:dec->base (/ n b) b) (LM:dec->base (rem n b) b))
  41.    )
  42. )

 
如果以上内容适用于您的语言,请尝试?
否则方案B:假设您使用UTF-8文件FSO读取流,它更稳定,但如果我有时间的话,很难配对1到4个字节
回复

使用道具 举报

mit

13

主题

33

帖子

20

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
65
发表于 2022-7-5 15:42:44 | 显示全部楼层
太棒了
它可以工作
 
非常感谢hanhphuc
谢谢李的密码
回复

使用道具 举报

5

主题

956

帖子

963

银币

初来乍到

Rank: 1

铜币
35
发表于 2022-7-5 15:50:42 | 显示全部楼层
 
不客气。希望你下次能自己编写代码
 
如果您对以前的unicode方法有疑问,下面是我的UTF-8函数在将来可能会有用。试试吧,祝你好运。。
  1. (alert (foo ret ) )
  2. [b][color="red"]ດິນບຸກຄົນ[/color][/b] ??
  3. [color="green"]"\U+0E94\U+0EB4\U+0E99\U+0E9A\U+0EB8\U+0E81\U+0E84\U+0EBB\U+0E99" [/color]

最后连接所有编码字节列表:
 
一些屏幕截图

                               
登录/注册后可看大图

 
随机测试阿拉伯语、汉语、印地语、日语、韩语、老挝语、旁遮普语、俄语、泰米尔语、越南语等。。还有一些问题
回复

使用道具 举报

mit

13

主题

33

帖子

20

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
65
发表于 2022-7-5 15:54:49 | 显示全部楼层
你好
云你能帮帮我吗?
这个代码怎么了?
 
  1. [color="green"];Reference, post#138
  2. ;https://stackoverflow.com/questions/643694/what-is-the-difference-between-utf-8-and-unicode[/color]
  3. (defun [color="blue"]UTF8->unicode[/color] ( l / ls 8b d2 foo) ; encode UTF-8 to unicode
  4. [color="green"];;;hanhphuc 17.04.2018 [/color]
  5. (setq        8b '((s) (while (< (strlen s)  (setq s (strcat "0" s))) s)
  6.         d2 '((str) ;split string to two list  
  7.                  (if (> (strlen str) 0)
  8.                    (cons (substr str 1  (d2 (setq str (substr str 9 ))))
  9.                    )
  10.              )
  11. foo '(($ / pos i) ; base2 to decimal
  12.                 (setq i 0)
  13.                 (+ (cond ((while (and (> (strlen $) 0) (setq pos (vl-string-search "1" $)))
  14.                               (setq         $ (substr $ (+ 2 pos))
  15.                                    i (+ i (expt 2 (strlen $)))
  16.                             )
  17.                            )
  18.                       )
  19.                    (0)
  20.                  )
  21.                        (atoi $)
  22.                            )
  23.              )
  24.         ls (mapcar ''((x / $)
  25.               (setq $ (LM:dec->base (foo x) 16))
  26.               (if
  27.                (= (strlen $) 1)
  28.                (strcat "0" $)
  29.                $
  30.                )
  31.               )
  32.            (d2
  33.              (apply 'strcat
  34.                     (mapcar ''((a x) (substr (8b a) (- 9 x) x))
  35.                             l
  36.                             (cdr (assoc (length l) '((1 . (7)) (2 . (5 6)) (3 . (4 6 6)) (4 . (3 6 6 6)))))
  37.                             )
  38.                     )
  39.              )
  40.            )
  41. )
  42. (apply 'strcat
  43. (vl-list* "\\U"
  44.           (if (> (length ls) 1)
  45.          "+"
  46.          "+00")
  47.        ls
  48.        )
  49. )
  50. )
  51. (defun [color="blue"]U8:bytes[/color] (l / x ls)
  52. [color="green"];hanhphuc 17.04.2018[/color]
  53. ;UTF-8 split the bytes
  54. (setq x (car l))
  55. (if l
  56.    (cons (vl-remove nil (cond        ((<= 0 x 191)
  57.          (setq ls (list x)
  58.                l  (cdr l)
  59.                )
  60.          ls
  61.          )
  62.         ((<= 192 x 223)
  63.          (setq ls (list x (cadr l))
  64.                l  (cddr l)
  65.                )
  66.          ls
  67.          )
  68.         ((<= 224 x 239)
  69.          (setq ls (list x (cadr l) (caddr l))
  70.                l  (cdddr l)
  71.                )
  72.          ls
  73.          )
  74.         ((<= 240 x 247)
  75.          (setq ls (list x (cadr l) (caddr l) (cadddr l))
  76.                l  (cddddr l)
  77.                )
  78.          ls
  79.          )
  80.         )
  81.     )
  82.   (U8:bytes l)
  83.   )
  84.    )
  85. )

 
所以换成这个
  1. (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]
  2.      )

愉快地编码
 
p/s:使用read char读取unicode文件
回复

使用道具 举报

5

主题

956

帖子

963

银币

初来乍到

Rank: 1

铜币
35
发表于 2022-7-5 16:01:33 | 显示全部楼层
非常感谢穆什·汉赫普克
回复

使用道具 举报

mit

13

主题

33

帖子

20

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
65
发表于 2022-7-5 16:09:28 | 显示全部楼层
不客气
 
对不起,参考链接中有#138的错别字,应该是#147的帖子
回复

使用道具 举报

5

主题

956

帖子

963

银币

初来乍到

Rank: 1

铜币
35
发表于 2022-7-5 16:15:19 | 显示全部楼层
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-7-9 19:52 , Processed in 0.813716 second(s), 75 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表