Hi,
I can't write unicode characters to a text file.
as you know, you can write chars on text screen easily:
(write-line (strcat "\\U+" "2122"))
but if you want to write chars to a text file, this command does not work:
(write-line (strcat "\\U+" "2122") TxtFileVar)
Does anybody knows the reason of this problem?
Here is a part of my code to test:
(defun C:HexGen ( / d f S w x y z h c) (setq d '("0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "A" "B" "C" "D" "E" "F") f (open "c:\\0000-FFFF.txt" "w") ) (foreach w d (foreach x d (foreach y d (progn (setq S "") (foreach z d (progn (setq h (strcat w x y z)) (setq c (strcat "\\U+" h));;;<===character (setq S (strcat S "\t" (strcat h " = " c ))) ) ) (write-line S f) ) ) ) ) (close f) (startapp "notepad" "c:\\0000-FFFF.txt") )
Thanks for your help.
Abbas
Solved! Go to Solution.
Solved by Lee_Mac. Go to Solution.
Abbas,
a silly question, the text file is created and opens?
Sometimes, with Win7, there are problems in writing directly in "C:/"...
I don't know why is not writing correctly in the text file, works fine for me...
Try
(setq c (strcat(chr 92)"U+" h)
Henrique
Hi Henrique,
Yes, The Text file created and opened successfully via Notepad.
But, No characters in text file, there are just code number of characters:
(part of created text file:)
...........
00A0 = \U+00A0 00A1 = \U+00A1 00A2 = \U+00A2 00A3 = \U+00A3 00A4 = \U+00A4......
...........
also, I tried your suggestion:
(setq c (strcat(chr 92)"U+" h)
and no differencies, same result!
Thanks,
Abbas
You need set your English system to non-English to wrtie Unicode characters directly into a txt file, for example set to Chinese to write Chinese characters. That's all. You have no other choice.
BTW, there is a website which can convert Unicode \U+nnn into Unicode characters, it is free and quick. Sorry I forget the website name. I used that website to convert before, not created from AutoCAD directly, just one more step and extra time.
@3wood wrote:That's all. You have no other choice.
Yes you do, you can write the file as a binary stream, e.g.:
(defun c:unicode-test ( / bt1 bt2 fso lst stm ) (repeat (setq bt1 256) (setq bt1 (1- bt1)) (repeat (setq bt2 256) (setq lst (vl-list* (setq bt2 (1- bt2)) bt1 lst))) ) (if (setq fso (vlax-create-object "scripting.filesystemobject")) (progn (vl-catch-all-apply (function (lambda ( ) (vlax-invoke (setq stm (vlax-invoke fso 'createtextfile (vl-filename-mktemp "uni" (getvar 'dwgprefix) ".txt") -1 0 ) ) 'write (apply 'strcat (mapcar 'chr (vl-list* 255 254 (subst 256 0 lst)))) ) (vlax-invoke stm 'close) ) ) ) (if (= 'vla-object (type stm)) (vlax-release-object stm) ) (if (= 'vla-object (type fso)) (vlax-release-object fso) ) ) )
(princ) ) (vl-load-com) (princ)
The above will create a little-endian Unicode text file in the working directory, containing Unicode characters 0x0000 - 0xFFFF
@3wood wrote:Nice code! But it looks the file is 0 bytes?
The file should be (and is in my tests) 128KB (FFFF = 65,536 chars x 2 bytes per char = 131,072 bytes = 128KB).
Stupid question guys: isn't there an "encoding" drop-down in Notepad (and settings in other applications) for controlling ANSI/Unicode/UTF-8/etc. format? Are you trying to open a Unicode file as ANSI?
@dgorsman wrote:Stupid question guys: isn't there an "encoding" drop-down in Notepad (and settings in other applications) for controlling ANSI/Unicode/UTF-8/etc. format? Are you trying to open a Unicode file as ANSI?
As you note, there are encoding options available when saving a file in Notepad (and other applications), however, to my knowledge Notepad will automatically detect the file encoding when opening a text file.
Hi Lee_Mac,
Thank you for your code, Can you please customize the output file as my lisp code in the 1st post?
;| This code genereates all characters in 0000-FFFF rang. Written By Lee_Mac on 2013.08.12 Output filename is uni.txt in defined path folder |; (defun c:unicode-test ( / bt1 bt2 fso lst stm path) (setq path "C:\\TEST\\") (repeat (setq bt1 256) (setq bt1 (1- bt1)) (repeat (setq bt2 256) (setq lst (vl-list* (setq bt2 (1- bt2)) bt1 lst))) );repeat (if (setq fso (vlax-create-object "scripting.filesystemobject")) (progn (vl-catch-all-apply (function (lambda ( ) (vlax-invoke (setq stm (vlax-invoke fso 'createtextfile (vl-filename-mktemp "uni" path ".txt") -1 0)) 'write (apply 'strcat (mapcar 'chr (vl-list* 255 254 (subst 256 0 lst)))) ) (vlax-invoke stm 'close) );lambda );function );vl-catch-all-apply (if (= 'vla-object (type stm))(vlax-release-object stm)) (if (= 'vla-object (type fso))(vlax-release-object fso)) );progn ) (princ) ) (vl-load-com) (princ)
Thanks a lot
Abbas
@aqdam1978 wrote:
Thank you for your code, Can you please customize the output file as my lisp code in the 1st post?
Please try the following:
;; Generates a text file containing all Unicode characters in the range x0000-xFFFF ;; Written by Lee Mac 2013-08-13 ;; Found at http://forums.autodesk.com/t5/Visual-LISP-AutoLISP-and-General/How-to-write-Unicode-characters-to-a-... (defun c:unicode-test ( ) (unicode-test (getvar 'dwgprefix)) ) (defun unicode-test ( dir / bt1 bt2 bt3 bt4 fso hex lst stm txt ) (setq hex (lambda ( x ) (+ x (if (< x 10) 48 55)))) (repeat (setq bt1 256) (setq bt1 (1- bt1)) (repeat (setq bt2 16) (setq bt2 (1- bt2)) (repeat (setq bt3 16) (setq bt3 (1- bt3) bt4 (+ (* 16 bt2) bt3) lst (vl-list* 09 256 92 256 85 256 43 256 (hex (/ bt1 16)) 256 (hex (rem bt1 16)) 256 (hex (/ bt4 16)) 256 (hex (rem bt4 16)) 256 32 256 61 256 32 256 bt4 bt1 lst ) ) ) (setq lst (vl-list* 13 256 10 256 (cddr lst))) ) ) (if (setq fso (vlax-create-object "scripting.filesystemobject")) (progn (vl-catch-all-apply (function (lambda ( ) (setq txt (vl-filename-mktemp "uni" dir ".txt") stm (vlax-invoke fso 'createtextfile txt -1 0) ) (vlax-invoke stm 'write (apply 'strcat (mapcar 'chr (vl-list* 255 254 (subst 256 0 (cddddr lst)))))) (vlax-invoke stm 'close) ) ) ) (if (= 'vla-object (type stm)) (vlax-release-object stm) ) (if (= 'vla-object (type fso)) (vlax-release-object fso) ) (if (findfile txt) (startapp "notepad" txt) ) ) ) (princ) ) (vl-load-com) (princ)
Please be aware that it may take a while to evaluate depending on your system hardware.
@aqdam1978 wrote:Thank you Lee_Mac, It's amazing!
You're most welcome - it was fun to write