Jump to content

Get Unicode of characters in LISP


Ahankhah

Recommended Posts

Hi all,

 

I have written a simple routine to convert "Unicode"s to equivalent "character"s

 

It is as following:

 

(defun MT:Conv:Unichar->Char    ;added: 94/12/12
      (%unichar% / *ret*)
(command "_.TEXT" '(0 0) "0.3" "0" %unichar%)
(setq *ret* (cdr (assoc 1 (entget (entlast)))))
(entdel (entlast))
*ret*
)

 

Example:

(MT:Conv:Unichar->Char "\\U+0647")    -- returns -->    "ه"

 

Is there any way to define a function to do the reverse?

I mean, it gets "character" as arguments and returns equivalent "Unicode"?

 

I appreciate any help or suggestions.:)

Link to comment
Share on other sites

  • 1 year later...

@Ahankhah:

The function MT:CONV:UNICHAR->CHAR doesn't do much... In a roundabout way it merely returns the input string.

(setq a (MT:CONV:UNICHAR->CHAR "\U+0647"))
(setq b "\U+0647")
(= a b) => T

My suggestion for your request:

; (String_To_UniCode "ABC") => "\\U+0041\\U+0042\\U+0043"
(defun String_To_UniCode (str)
 (apply
   'strcat
   (mapcar
     '(lambda (int / hex)
       (setq hex (KGA_Math_Dec_To_Hex int))
       (repeat (- 4 (strlen hex))
         (setq hex (strcat "0" hex))
       )
       (strcat "\\U+" hex)
     )
     (vl-string->list str)
   )
 )
)

(defun KGA_Math_Dec_To_Base (int base / lst rest)
 (if (zerop int)
   "0"
   (progn
     (while (> int 0)
       (setq
         rest (rem int base)
         lst (cons (+ rest (if (<= rest 9) 48 55)) lst)
         int (/ int base)
       )
     )
     (vl-list->string lst)
   )
 )
)

; (KGA_Math_Dec_To_Hex 2147483647) => "7FFFFFFF"
; (KGA_Math_Dec_To_Hex 14285)      => "37CD"
; (KGA_Math_Dec_To_Hex 0)          => "0"
(defun KGA_Math_Dec_To_Hex (int)
 (KGA_Math_Dec_To_Base int 16)
)

  • Like 1
Link to comment
Share on other sites

(vl-string->list "ABC") does return (65 66 67). For ASCII characters, it works.

What about international characters?

(vl-string->list "ه") returns (92 85 43 48 54 52 55), which turns out to be \U+0647

And dismayingly, in my dwgcodepage ANSI_936, (vl-string->list "啊") returns (176 161) rather than its unicode which should be \U+554A

  • Like 1
Link to comment
Share on other sites

@honglog:

It seems that BricsCAD (the program I use) and AutoCAD have a different implementation of vl-string->list and perhaps also of the ascii function.

 

Some tests with BricsCAD:

(setq a "\U+0647")
(ascii a) => 1607
(vl-string->list a) => (1607)
(String_To_UniCode a) => "\\U+0647"

(setq b "\U+554A")
(ascii b) => 21834
(vl-string->list b) => (21834)
(String_To_UniCode b) => "\\U+554A"

  • Like 1
Link to comment
Share on other sites

  • 1 year later...

I HAVE A FILE NAME IN UNICODE :(SETQ QFILE "D:\\\U+05D3\U+05D5\U+05E8\U+05D5\U+05DF\\DRAWING3.DWG")

I CAN'T OPEN A NEW FILE

(setq f (open qfile "w")

  • Like 1
Link to comment
Share on other sites

I HAVE A FILE NAME IN UNICODE :(SETQ QFILE "D:\\\U+05D3\U+05D5\U+05E8\U+05D5\U+05DF\\DRAWING3.DWG")

I CAN'T OPEN A NEW FILE

(setq f (open qfile "w")

hi, you only can write to asciii file, e.g .dxf, txt, csv etc.., but not a drawing file

 

do you mean open a new drawing?

 

just curious ac2007

 ([color="blue"]ascii[/color] "\U+6C49") [color="green"];returns only 92 the first char[/color]

  • Like 1
Link to comment
Share on other sites

hi, you only can write to asciii file, e.g .dxf, txt, csv etc.., but not a drawing file

 

do you mean open a new drawing?

 

just curious ac2007

 ([color="blue"]ascii[/color] "\U+6C49") [color="green"];returns only 92 the first char[/color]

=================================================

vlisp return the name of the folder in Unicode ?!?

(setq a (strcat (getvar "dwgprefix")(setvar "dwgname"))) = D:\\\U+05D3\U+05D5\U+05E8\U+05D5\U+05DF\\DRAWING3 .DWG"

because the folder name is in Hebrew : "D:\\דורון\\DRAWING3.DWG"

I found the solution:

Problem displaying Arabic letters on windows - YouTube

ttps://www.youtube.com/watch?v=XkczYaBlbNY

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...