home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / wp / bmacs.zip / HTOA.M < prev    next >
Text File  |  1989-01-01  |  2KB  |  76 lines

  1. ; Macro to convert a hexadecimal string to ASCII and insert it at the
  2. ; cursor position--contributed by Michael A. Fuller
  3. ;
  4. ; We at Solution Systems would like it if someone would take this,
  5. ; and adapt it to read a char from the current buffer and return the ASCII
  6. ; (integer) value of that character.  That way, we'd have the rudiments
  7. ; of a hex-editing system (together with sprintf "%x")!
  8. ; Enter the hex values without spaces between them.
  9.  
  10. (macro htoa
  11.     (
  12.         (int h_count h_upper h_value h_pass h_rc)
  13.         (string work_string build_string out_string)
  14.  
  15.         (save_position)
  16.         (if (get_parm 0 work_string "Enter hex values: ")
  17.             (
  18.                 (if (!= 0 (% (strlen work_string) 2))
  19.                     (
  20.                         (error "Odd number of values.  Please retry")
  21.                         (restore_position)
  22.                     )
  23.                 ;else
  24.                     (
  25.                         (while (< h_count (strlen work_string))
  26.                             (
  27.                                 (= h_pass 0)
  28.                                 (while (<= h_pass 1)
  29.                                     (
  30.                                         (= h_value (atoi (substr work_string (+ (+ 1 h_pass) h_count) 1) 0))
  31.                                         (if (&& (>= h_value 48) (<= h_value 57))
  32.                                             (-= h_value 48)
  33.                                         ;else
  34.                                             (
  35.                                                 (if (&& (>= h_value 65) (<= h_value 70))
  36.                                                     (-= h_value 55)
  37.                                                 ;else
  38.                                                     (
  39.                                                         (if (&& (>= h_value 97) (<= h_value 102))
  40.                                                             (-= h_value 87)
  41.                                                         ;else
  42.                                                             (
  43.                                                                 (error "Illegal value: %c" h_value)
  44.                                                                 (+= h_count (strlen work_string))
  45.                                                                 (= h_rc 1)
  46.                                                             )
  47.                                                         )
  48.                                                     )
  49.                                                 )
  50.                                             )
  51.                                         )
  52.                                         (if (== h_pass 0)
  53.                                             (= h_upper h_value)
  54.                                         )
  55.                                         (+= h_pass 1)
  56.                                     )
  57.                                 )
  58.                                 (sprintf build_string "%c" (+ h_value (* 16 h_upper)))
  59.                                 (+= out_string build_string)
  60.                                 (+= h_count 2)
  61.                             )
  62.                         )
  63.                         (restore_position)
  64.                         (if (== h_rc 0)
  65.                             (insert out_string)
  66.                         )
  67.                     )
  68.                 )
  69.             )
  70.         ;else
  71.             (restore_position)
  72.         )
  73.     )
  74. )
  75.         
  76.