Author Topic: About function CHAR  (Read 234 times)

0 Members and 1 Guest are viewing this topic.

baitang36

  • Newt
  • Posts: 115
About function CHAR
« on: September 09, 2021, 10:08:39 PM »
The function converts an integer representing an ASCII character code into a single character string
(Chr 1) return "\ 001"
(Chr 2) return "\ 002"
(Chr 3) return "\ 003"
What does (Chr (- 9 1)) return?
You may not hesitate to say "\008", but you are wrong and return "\ 010".
Similarly, (Chr 0) returns an empty character instead of "\ 000".
(Chr 11) return "\ 013"
In other words, the return value of this function does not have a one-to-one correspondence with the ASCII code. The number after the slash is not necessarily ASCII code, and some numbers do not have corresponding characters.
Therefore, using write-char and char functions to write binary files is very risky.
To write binary files, use the function write-byte, which is a reserved function.

The following are the test data, (Chr 0) to (Chr 127)
"" "\001" "\002" "\003" "\004" "\005" "\006" "\007" "\010" "\t" "\n" "\013" "\014" "\r" "\016" "\017" "\020" "\021" "\022" "\023" "\024" "\025" "\026" "\027" "\030" "\031" "\032" "\e" "\034" "\035" "\036" "\037" " " "!" "\"" "#" "$" "%" "&" "'" "(" ")" "*" "+" "," "-" "." "/" "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" ":" "; " "<" "=" ">" "? " "@" "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z" "[" "\\" "]" "^" "_ " "`" "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z" "{" "|" "}" "~" "\177" "

MatGrebe

  • Mosquito
  • Posts: 10
Re: About function CHAR
« Reply #1 on: September 10, 2021, 02:13:47 AM »
I would say, the output is octal.

So (- 9 1) is 8 which is 1x8+0x1 (001) and 127 becomes 1*8*8+7*8+7*1 (177)

Mathias

baitang36

  • Newt
  • Posts: 115
Re: About function CHAR
« Reply #2 on: September 10, 2021, 02:27:43 AM »
I would say, the output is octal.

So (- 9 1) is 8 which is 1x8+0x1 (001) and 127 becomes 1*8*8+7*8+7*1 (177)

Mathias
(chr 256)return "\000"

Lee Mac

  • Seagull
  • Posts: 12514
  • London, England
Re: About function CHAR
« Reply #3 on: September 10, 2021, 01:45:23 PM »
As correctly asserted by MatGrebe, and also described by the documentation, "\nnn" represents the ASCII character with octal code nnn. In your case, 0108 is equal to 00810.

As for (chr 256), the chr function accepts an integer between 1-255; anything above this is interpreted mod 256, e.g.:
Code - Auto/Visual Lisp: [Select]
  1. _$ (chr (+ 256 65))
  2. "A"

baitang36

  • Newt
  • Posts: 115
Re: About function CHAR
« Reply #4 on: September 12, 2021, 05:18:59 AM »
As correctly asserted by MatGrebe, and also described by the documentation, "\nnn" represents the ASCII character with octal code nnn. In your case, 0108 is equal to 00810.

As for (chr 256), the chr function accepts an integer between 1-255; anything above this is interpreted mod 256, e.g.:
Code - Auto/Visual Lisp: [Select]
  1. _$ (chr (+ 256 65))
  2. "A"
thank you