ASCII コードは、アルファベット文字を表す整数値です。
0 から 127 までの数値が、0 から 9 までの数字、アルファベット文字、句読点、およびその他の特殊文字を表します。一部の AutoLISP 関数は、該当する文字列の代わりに、単一の文字に対する ASCII コードを返したり、受け入れます。
たとえば grread 関数は、ユーザがキーボード入力を行うと、押されたキーの ASCII コードを返します。次に、プログラムでどのように使用するかに基づいて、返されたリスト内の ASCII コードを評価または変換して表示することができます。
次に、ASCII コードを扱うときに使用する AutoLISP 関数を示します。
ascii 関数と chr 関数を使用して、ASCII コードの変換を行うことができます。ascii 関数は、文字列に対応する ASCII 値を返し、chr 関数は、ASCII 値に対応する文字を返します。
次のコードは、大文字の A を ASCII コード値に変換します。
(ascii "A") 65
次のコードは、ASCII コード値をそれが表している文字に変換します。
(chr 65) "A"
次のサンプル コードは、ASCII という名前のコマンドと、ASCII コマンドが使用する BASE という名前の関数を定義します。ASCII コマンドは、10 進表記、8 進表記、16 進表記のコードを使用して、文字をスクリーンおよび ASCII.txt というファイルに書き出します。
; BASE converts from a decimal integer to a string in another base.
(defun BASE (bas int / ret yyy zot)
(defun zot (i1 i2 / xxx)
(if (> (setq xxx (rem i2 i1)) 9)
(chr (+ 55 xxx))
(itoa xxx)
)
)
(setq ret (zot bas int)
yyy (/ int bas)
)
(setq ret (strcat (zot bas yyy) ret))
(setq yyy (/ yyy bas))
(strcat (zot bas yyy) ret)
)
(defun C:ASCII (/) ;chk out ct code dec oct hex )
(initget "Yes")
(setq chk (getkword "\nWriting to ASCII.TXT, continue? <Y>: "))
(if (or (= chk "Yes") (= chk nil))
(progn
(setq out (open "ascii.txt" "w")
chk 1
code 0
ct 0
)
(princ "\n \n CHAR DEC OCT HEX \n")
(princ "\n \n CHAR DEC OCT HEX \n" out)
(while chk
(setq dec (strcat " " (itoa code))
oct (base 8 code)
hex (base 16 code)
)
(setq dec (substr dec (- (strlen dec) 2) 3))
(if (< (strlen oct) 3)
(setq oct (strcat "0" oct))
)
(princ (strcat "\n " (chr code) " " dec " " oct " " hex))
(princ (strcat "\n " (chr code) " " dec " " oct " " hex)
out
)
(cond
((= code 255) (setq chk nil))
((= ct 20)
(setq
xxx (getstring
"\n \nPress 'X' to eXit or any key to continue: "
)
)
(if (= (strcase xxx) "X")
(setq chk nil)
(progn
(setq ct 0)
(princ "\n \n CHAR DEC OCT HEX \n")
)
)
)
)
(setq ct (1+ ct)
code (1+ code)
)
)
(close out)
(setq out nil)
)
)
(princ)
)