Jump to content

Ascii and (vl-string-elt) give wrong answers!!


grandhougday

Recommended Posts

I want to write a lisp that read a text from model space and replace some characters in it. for this work i iterate each character and take Ascii code of each one, then in if-else do replacement. in some chars it get wrong answers. for example in character "Þ" (latin capital letter thorn)(Ascii code:222 or Hex=\U+00DE).

both (vl-string-elt "Þ" 0) and (Ascii "Þ") commands give me 92 as a resault of it ascii code :cry:

i use doslib, but same problem exist.

does any one know what's the problem?

Link to comment
Share on other sites

  • Replies 27
  • Created
  • Last Reply

Top Posters In This Topic

  • Lee Mac

    11

  • grandhougday

    9

  • Ahankhah

    5

  • marko_ribar

    3

Top Posters In This Topic

may be something strange is wrong with me. i tried in XP SP3, and win7(64), with Acad2009, Acad2012 and 2011 All give me :

Command: (ascii "Þ")

92

Link to comment
Share on other sites

I'm really confused with this:

 

_$ Command: (ascii "Þ")

92

nil

222

92

_$ (Ascii "Þ")

222

_$ (Ascii "Þ")

222

_$ (Ascii "Þ")

222

in the first run VLIDE was also confused and in later run it got the true answer.

 

When I paste from charmap, char turn into question mark and return equivalent ascii that is 63

_$ (ascii "?")

63

and still in command prompt it say's 92.

I have a second language installed. is it the cause?

Link to comment
Share on other sites

Firstly, the capital in (Ascii) will make no difference since LISP isn't case-sensitive.

 

Now:

_$ Command: (ascii "Þ")
92
nil
222
92

This result is because you have copied 'Command:' to the console as well, which is interpreted as a null symbol:

 

_$ Command: (ascii "Þ")
nil
222

 

_$ Command:
nil

 

Though, I'm not sure where the ASCII 92 (backslash "\") codes are coming from.

 

As for:

 

_$ (ascii "?")
63

63 is simply the ASCII code for the question mark. LISP can only process ASCII characters, so any Unicode characters copied into the console will be converted to question marks.

Link to comment
Share on other sites

in cases that i face with these kind of problem (i mean this char and some other char) in the following code txt1 and txt2 give different answers:

(setq item (ssname sset i))
(setq ed (entget item))
(setq VlObj (vlax-ename->vla-object item))
(setq txt2 (vla-get-textstring VlObj))
(setq txt1 (cdr(assoc 1 ed)))

Link to comment
Share on other sites

Yes, there is a known bug wherein Extended ASCII and Unicode symbols will be replaced with question marks '?' when the text content is retrieved using the TextString property of the VLA-Object.

 

When the Text object contains such characters, always use the DXF Group codes.

Link to comment
Share on other sites

I found out what is the meaning of result of (vl-string-elt). in command prompt i get this answer:

Command: (vl-string-elt "Þ" 0)
92

Command: (vl-string-elt "Þ" 1)
85

Command: (vl-string-elt "Þ" 2)
43

Command: (vl-string-elt "Þ" 3)
48

Command: (vl-string-elt "Þ" 4)
48

Command: (vl-string-elt "Þ" 5)
68

Command: (vl-string-elt "Þ" 6)
69

that means :\U+00DE that means "222"

can anyone know why it does like this? and how can get right answer?

Link to comment
Share on other sites

OK, so from that, I'm guessing that this:

 

(vl-list->string (vl-string->list "Þ")

 

Will return:

 

"\U+00DE"

 

With this in mind we can convert the Hexadecimal part (00DE) to a Decimal representation:

 

(defun _ascii ( ch )
   (_HexList->Decimal (reverse (cdddr (vl-string->list ch))))
)

(defun _HexList->Decimal ( lst )
   (if lst
       (+  (* 16 (_HexList->Decimal (cdr lst)))
           (if (< (car lst) 65)
               (- (car lst) 48)
               (- (car lst) 55)
           )
       )
       0
   )
)

 

_$ (_ascii "Þ")
222

Link to comment
Share on other sites

Lee,

let me edit your excellent code so the internal "ascii" function can be replaced with the new one:

 

 
[size=3][font=Calibri](defun _ascii (ch / ret)[/font][/size]
[size=3][font=Calibri]  (if (zerop (setq ret (_HexList->Decimal (reverse (cdddr (vl-string->list ch))))))[/font][/size]
[size=3][font=Calibri]    (ascii ch)[/font][/size]
[size=3][font=Calibri]    ret[/font][/size]
[size=3][font=Calibri]  ) [/font][/size]
[size=3][font=Calibri])[/font][/size]

[size=3][font=Calibri](defun _HexList->Decimal (lst)[/font][/size]
[size=3][font=Calibri]  (if lst[/font][/size]
[size=3][font=Calibri]    (+ (* 16 (_HexList->Decimal (cdr lst)))[/font][/size]
[size=3][font=Calibri]      (if (< (car lst) 65)[/font][/size]
[size=3][font=Calibri]        (- (car lst) 48)[/font][/size]
[size=3][font=Calibri]        (- (car lst) 55)[/font][/size]
[size=3][font=Calibri]      )[/font][/size]
[size=3][font=Calibri]    )[/font][/size]
[size=3][font=Calibri]    0[/font][/size]
[size=3][font=Calibri]  )[/font][/size]
[font=Calibri][size=3])[/size][/font]

Edited by Ahankhah
Link to comment
Share on other sites

This is some sort of wrong result - on my comp it's correct :

 

Command: (vl-string->list "Þ")
(222)

M.R.:)

 

But on my computer:

 

in AutoCAD command line:

Command: (vl-string->list "Þ")
(92 85 43 48 48 68 69)

 

and in Vlide console:

_$ (vl-string->list "?")
(63)

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