Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to write Unicode characters to a text file?

19 REPLIES 19
SOLVED
Reply
Message 1 of 20
aqdam1978
6821 Views, 19 Replies

How to write Unicode characters to a text file?

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

 

 

 

 

 

 

 

 

 

 

19 REPLIES 19
Message 2 of 20
3wood
in reply to: aqdam1978

Have your set the Windows system code page to English?

Message 3 of 20
aqdam1978
in reply to: 3wood

Hi 3wood,

 

Yes, it's English, U.S.

 

Thanks

 

Message 4 of 20
hmsilva
in reply to: aqdam1978

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

EESignature

Message 5 of 20
aqdam1978
in reply to: hmsilva

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

 

Message 6 of 20
hmsilva
in reply to: aqdam1978

Sorry Abbas, I misunderstood your question, cant be much help... Smiley Embarassed

 

Henrique

EESignature

Message 7 of 20
aqdam1978
in reply to: aqdam1978

Hi,

 

There is no idea/solution?!

Nobody can help?

Message 8 of 20
3wood
in reply to: aqdam1978

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.

Message 9 of 20
Lee_Mac
in reply to: 3wood


@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

Message 10 of 20
3wood
in reply to: Lee_Mac

Nice code! But it looks the file is 0  bytes?

Message 11 of 20
Lee_Mac
in reply to: 3wood


@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).

Message 12 of 20
dgorsman
in reply to: Lee_Mac

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?

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


Message 13 of 20
Lee_Mac
in reply to: dgorsman


@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.

Message 14 of 20
aqdam1978
in reply to: Lee_Mac

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

Message 15 of 20
Lee_Mac
in reply to: aqdam1978


@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.

Message 16 of 20
aqdam1978
in reply to: Lee_Mac

Thank you Lee_Mac, It's amazing!

Message 17 of 20
Lee_Mac
in reply to: aqdam1978


@aqdam1978 wrote:

Thank you Lee_Mac, It's amazing!


You're most welcome - it was fun to write Smiley Happy

Message 18 of 20
hmsilva
in reply to: Lee_Mac


@Lee_Mac wrote:

... it was fun to write Smiley Happy


 

Great thinking!

Nicely coded, Lee.

 

Henrique

EESignature

Message 19 of 20
Lee_Mac
in reply to: hmsilva


@hmsilva wrote:
Great thinking!

Nicely coded, Lee.

 

Henrique


 

Thank you Henrique! Smiley Happy

Message 20 of 20
hadvpecc3
in reply to: Lee_Mac

How to write Unicode characters to a dialog or a alert?

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost