CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

AutoLISP 开发指南

关于 ASCII 代码(AutoLISP)

2023-1-6 12:17| 发布者: admin| 查看: 902| 评论: 0|来自: AutoCAD

摘要: ASCII 代码是表示字母数字字符的整数值。

ASCII 代码是表示字母数字字符的整数值。

数值 0 到 127 表示数字 0 到 9、字母表中的字母、标点符号和其他特殊字符。某些 AutoLISP 函数返回或期望单个字符的 ASCII 代码,而不是等效的字符串。

例如,该函数返回用户提供键盘输入时按下的键的 ASCII 代码。然后,可以根据程序将如何使用它来评估或转换返回的列表中的 ASCII 代码以显示。grread

以下是使用 ASCII 代码时使用的一些 AutoLISP 函数:

  • grread– 从任何 AutoCAD 输入设备读取值。
  • read-char– 返回表示从键盘输入缓冲区或打开的文件读取的字符的 ASCII 代码。
  • write-char– 将一个字符写入屏幕或打开的文件。
  • ascii– 返回字符串第一个字符的 ASCII 代码。
  • chr– 将表示 ASCII 字符代码的整数转换为单字符字符串。

将 ASCII 代码和字符转换为 ASCII 代码

Theand函数可用于处理ASCII码的转换。该函数返回与字符串关联的 ASCII 代码,并返回与 ASCII 代码关联的字符串。asciichrasciichr

以下代码将大写 A 转换为其 ASCII 代码值:

(ascii "A")
65

以下代码将 ASCII 代码值转换为它表示的字符串:

(chr 65)
"A"

下面的示例代码定义一个名为 ded 的命令和一个名为该命令使用的函数。该命令将字符及其代码以十进制、八进制和十六进制形式写入屏幕和名为ASCII.txt 的文件。ASCIIBASEASCIIASCII

; 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)
)

路过

雷人

握手

鲜花

鸡蛋

最新评论

AutoCAD VBA参数化绘图程序开发与实战

QQ|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1 )

GMT+8, 2024-5-6 19:59

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部