TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: baitang36 on August 04, 2021, 08:33:19 PM

Title: How to make (Chr 0) return "\000" ?
Post by: baitang36 on August 04, 2021, 08:33:19 PM
;The result of this program is wrong because (Chr 0) returns an empty string, resulting in the loss of bytes with a value of 0
Code - Auto/Visual Lisp: [Select]
  1. (defun tran1 (str1)
  2.   (cond
  3.     ((= str1 "0") (setq b1 0))
  4.     ((= str1 "1") (setq b1 1))
  5.     ((= str1 "2") (setq b1 2))
  6.     ((= str1 "3") (setq b1 3))
  7.     ((= str1 "4") (setq b1 4))
  8.     ((= str1 "5") (setq b1 5))
  9.     ((= str1 "6") (setq b1 6))
  10.     ((= str1 "7") (setq b1 7))
  11.     ((= str1 "8") (setq b1 8))
  12.     ((= str1 "9") (setq b1 9))
  13.     ((= str1 "a") (setq b1 10))
  14.     ((= str1 "b") (setq b1 11))
  15.     ((= str1 "c") (setq b1 12))
  16.     ((= str1 "d") (setq b1 13))
  17.     ((= str1 "e") (setq b1 14))
  18.     ((= str1 "f") (setq b1 15))
  19.     (t (setq b1 0))
  20.   ) ;_ cond
  21.   b1
  22. ) ;_ defun
  23.  
  24.  
  25. (setq fi (open "d:/t2.txt" "R"))
  26. (setq buffer (READ-LINE fi))
  27. (close fi)
  28.  
  29. (setq n (/ (strlen buffer) 2))
  30. (setq i 1)
  31. (setq str2 "")
  32.   (setq a1 (substr buffer i 1))
  33.   (setq a2 (substr buffer (+ i 1) 1))
  34.   (setq c1 (tran1 a1))
  35.   (setq c2 (tran1 a2))
  36.   (setq c3 (+ (* 16 c1) c2))
  37.  
  38.   (setq strc3 (chr c3))         ;error!
  39.   (setq str2 (strcat str2 strc3))
  40.  
  41.   (setq i (+ i 2))
  42. ) ;_ repeat
  43.  
  44. (setq fo (open "d:/t2.fas" "w"))
  45. (princ str2 fo)
  46. (close fo)
  47.  
Title: Re: How to make (Chr 0) return "\000" ?
Post by: BIGAL on August 04, 2021, 09:43:21 PM
So your trying to convert a fas hex dump to a ascii readable format. Is it to decrypt a file ?
Title: Re: How to make (Chr 0) return "\000" ?
Post by: baitang36 on August 04, 2021, 09:57:47 PM
So your trying to convert a fas hex dump to a ascii readable format. Is it to decrypt a file ?
Yes, I'm experimenting
Title: Re: How to make (Chr 0) return "\000" ?
Post by: JohnK on August 04, 2021, 10:12:49 PM
I'm not sure you can. I want to say that the interpreter will always substitute "\000" as "".

I want to say something like:
Code - Auto/Visual Lisp: [Select]
  1. (setq strc3
  2.  (cond
  3.   ((not (eq (chr c3) "")) (chr c3))
  4.     ('\000)
  5.   )
  6.  )
but I don't think it will work (if it does, great!).
Title: Re: How to make (Chr 0) return "\000" ?
Post by: baitang36 on August 05, 2021, 12:13:52 AM
I'm not sure you can. I want to say that the interpreter will always substitute "\000" as "".

I want to say something like:
Code - Auto/Visual Lisp: [Select]
  1. (setq strc3
  2.  (cond
  3.   ((not (eq (chr c3) "")) (chr c3))
  4.     ('\000)
  5.   )
  6.  )
but I don't think it will work (if it does, great!).
it returns a symbol  \\ 000
Change ('\ 000) to ("\ 000") returns an empty string
Title: Re: How to make (Chr 0) return "\000" ?
Post by: baitang36 on August 05, 2021, 08:10:27 PM
A friend helped me solve this problem by replacing (Chr 0) with (Chr 256). Thank you

The source code that can be used normally is as follows :
Code - Auto/Visual Lisp: [Select]
  1. (defun tran1 (str1)
  2.   (cond
  3.     ((= str1 "0") (setq b1 0))
  4.     ((= str1 "1") (setq b1 1))
  5.     ((= str1 "2") (setq b1 2))
  6.     ((= str1 "3") (setq b1 3))
  7.     ((= str1 "4") (setq b1 4))
  8.     ((= str1 "5") (setq b1 5))
  9.     ((= str1 "6") (setq b1 6))
  10.     ((= str1 "7") (setq b1 7))
  11.     ((= str1 "8") (setq b1 8))
  12.     ((= str1 "9") (setq b1 9))
  13.     ((= str1 "a") (setq b1 10))
  14.     ((= str1 "b") (setq b1 11))
  15.     ((= str1 "c") (setq b1 12))
  16.     ((= str1 "d") (setq b1 13))
  17.     ((= str1 "e") (setq b1 14))
  18.     ((= str1 "f") (setq b1 15))
  19.     (t (setq b1 0))
  20.   ) ;_ cond
  21.   b1
  22. ) ;_ defun
  23.  
  24.  
  25. (setq fi (open "d:/t2.txt" "R"))
  26. (setq buffer (READ-LINE fi))
  27. (close fi)
  28.  
  29. (setq n (/ (strlen buffer) 2))
  30. (setq i 1)
  31. (setq str2 "")
  32.   (setq a1 (substr buffer i 1))
  33.   (setq a2 (substr buffer (+ i 1) 1))
  34.   (setq c1 (tran1 a1))
  35.   (setq c2 (tran1 a2))
  36.   (setq c3 (+ (* 16 c1) c2))
  37.  
  38.  (if (= c3 0)
  39.     (setq strc3 (chr 256))   ;It is modified here
  40.     (setq strc3 (chr c3))
  41.   )  
  42.   (setq str2 (strcat str2 strc3))
  43.  
  44.   (setq i (+ i 2))
  45. ) ;_ repeat
  46.  
  47. (setq fo (open "d:/t3.fas" "w"))
  48. (princ str2 fo)
  49. (close fo)
Title: Re: How to make (Chr 0) return "\000" ?
Post by: JohnK on August 05, 2021, 09:14:18 PM
Nice! Thank you very much for posting the solution. I really appreciate that.
Title: Re: How to make (Chr 0) return "\000" ?
Post by: baitang36 on August 05, 2021, 10:52:06 PM
Nice! Thank you very much for posting the solution. I really appreciate that.
Thank you