Author Topic: Number to Corresponding Letter  (Read 1472 times)

0 Members and 1 Guest are viewing this topic.

cmwade77

  • Swamp Rat
  • Posts: 1443
Number to Corresponding Letter
« on: March 24, 2022, 01:01:14 PM »
I am trying tow write a function that will take a number and convert it into the corresponding letter of the alphabet, that part is easy:
Code: [Select]
(defun NumberToLetter (Number / Prefix Results)
    (cond
      ((= LetterCount 0)
        (setq Results (chr (atoi (rtos (+ Number 64) 2 0))))
      )
    )
    Results
  )
What I also want is say after hitting Z, (i.e. using number 27 and up), it should then return something like:
AA
AB
AC

And if hitting something beyond 52, it should then go to:
BA
BB
BC

So on and so forth.

That is where I am getting stumped on how to approach it.


I would also like to go in revers and say someone provided a string of AA, I would want to return number 27.
« Last Edit: March 24, 2022, 01:11:35 PM by cmwade77 »

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: Number to Corresponding Letter
« Reply #1 on: March 24, 2022, 01:11:26 PM »
I would use a modulus to achieve the second char.

Code - Auto/Visual Lisp: [Select]
  1. (defun modulo (n d)
  2.    ;; Function: scm_modulo (n, d)
  3.    ;;   Return the remainder from n divided by d, with
  4.    ;;   the same sign as d.
  5.    ;;
  6.    ;; (modulo 13 4) > 1
  7.    ;; (modulo -13 4) > 3
  8.    ;; (modulo 13 -4) > -3
  9.    ;; (modulo -13 -4) > -1
  10.     (cond
  11.       ((and (minusp n) (not (minusp d)))
  12.        (1+ (~ (/ n d))))
  13.       ((minusp d) (1- (~ (rem n d))))
  14.        ((rem n d))) )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Number to Corresponding Letter
« Reply #2 on: March 24, 2022, 01:40:12 PM »
I ended up finding a different way around the issue.

kirby

  • Newt
  • Posts: 127
Re: Number to Corresponding Letter
« Reply #3 on: March 24, 2022, 01:43:22 PM »
Here is what I use, but the cool kids say that my code is too long...

Code - Auto/Visual Lisp: [Select]
  1. (defun C:TestAInc ()
  2. ; Test function for 'AlphaIncrement'
  3. ; KJM - March 2019
  4.  
  5. (if (eq MyString nil) (setq MyString "A"))
  6. (setq MyString (getdata "S" "Enter a 1 or 2 character alpha string " MyString))
  7.  
  8. (setq NewString (AlphaIncrement MyString 1))
  9. (prompt "\n  Original = ")(princ MyString)(prompt "  New = ")(princ NewString)
  10.  
  11. (setq MyString NewString)
  12. )
  13.  
  14.  
  15.  
  16. (defun AlphaIncrement (MyString UCase /
  17.                         Part1Char Part2Char Part1 Part2 NextValue NewString
  18.                         )
  19. ; Make alpha value and increment, eg. given A return B, given Z return AA, given AB return AC, etc
  20. ; KJM - March 2019
  21. ; Input:
  22. ;       MyString - (string) starting character(s), may be multiple character length eg. A or AB
  23. ;               initialize with nil or ""
  24. ;       UCase - (integer) 1 to return uppercase, 0 lower case
  25. ; Returns:
  26. ;       string incremented by next alpha character
  27. ;       when Z is reached, next character is AA
  28. ;       does not mix upper and lower case, does not go beyond ZZ (maximum value with 2 characters)
  29.  
  30. ; https://support.microsoft.com/en-ca/help/833402/how-to-convert-excel-column-numbers-into-alphabetical-characters
  31.  
  32.  
  33. ; Uppercase ascii A = 65 and ascii Z = 90, Lowercase ascii a = 97 and ascii z = 122
  34.  
  35. (if (or (eq MyString "") (eq MyString nil))
  36.   (progn
  37.         ; Initialize
  38.         (setq NewString "A")
  39.   )
  40.   (progn
  41.        
  42.         ; Convert to upper case
  43.         (setq MyString (strcase MyString))
  44.  
  45.         ; Separate two character string for processing
  46.         (if (> (strlen MyString) 1)
  47.           (progn
  48.                 (setq Part1Char (substr MyString 1 (1- (strlen MyString))))
  49.                 (setq Part2Char (substr MyString (strlen MyString)))
  50.           )
  51.           (progn
  52.                 (setq Part1Char "")
  53.                 (setq Part2Char MyString)
  54.           )
  55.         )
  56.  
  57.         ; Convert string to integer
  58.         (setq Part1 (ascii Part1Char))  ; (ascii "" = 0)
  59.         (setq Part2 (ascii Part2Char)) 
  60.  
  61.         (setq NextValue (+ Part2 1))
  62.         (if (> NextValue 90)
  63.           (progn
  64.                 ; increment first part
  65.                 (if (eq Part1 0)
  66.                         (setq Part1 65)
  67.                         (setq Part1 (1+ Part1))
  68.                 )
  69.                 (setq Part2 65)
  70.           )
  71.           (progn
  72.                 (setq Part2 NextValue)
  73.  
  74.           )
  75.         ) ; close if
  76.  
  77.         ; Rebuild string.  Note chr 0 = ""
  78.         (setq NewString (strcat (chr Part1) (chr Part2)))
  79.   )
  80. ) ; close if
  81.  
  82. ; Convert to lower case if desired
  83. (if (eq UCase 0)
  84.         (setq NewString (strcase NewString 1))
  85. )
  86.  
  87. NewString
  88. )
  89.  
  90.  

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: Number to Corresponding Letter
« Reply #4 on: March 24, 2022, 07:02:42 PM »
I ended up finding a different way around the issue.

Would you care to share?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Number to Corresponding Letter
« Reply #5 on: March 24, 2022, 07:55:22 PM »
I ended up finding a different way around the issue.

Would you care to share?
Yes, I was able to find code on LeeMac's site that allowed me to increment values and I was able to rework some code accordingly.

In short, I have been trying to be able to add numbered and lettered (as well as bulleted) lists to tables since Autodesk doesn't seem to think they need to add the feature.

I have come up with a way to accomplish this, please see the attached files, the blocks should be placed in one of your support paths.

There is a lot more than just the adding of lists, but to add a list, use the addlist command.

Please note that I have tested this with the tables that are generated by this routine, I have not tested it with others, but it likely will not work without modification on other tables, so you should use makegn or makern to use tables to test with. On other table styles, if you add $List as a cell style, it will likely work as well.

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Number to Corresponding Letter
« Reply #6 on: March 29, 2022, 05:52:15 PM »
Here are some alternatives you may wish to consider.

BIGAL

  • Swamp Rat
  • Posts: 1409
  • 40 + years of using Autocad
Re: Number to Corresponding Letter
« Reply #7 on: March 29, 2022, 08:40:36 PM »
Like Lee another example is inside Getxecel.lsp the 2 functions by Gile.

; ColumnRow - Returns a list of the Column and Row number

; Alpha2Number - Converts Alpha string into Number
A man who never made a mistake never made anything