Basic String-Number Conversion

Basic String-Number Conversion

Anonymous
Not applicable
4,794 Views
21 Replies
Message 1 of 22

Basic String-Number Conversion

Anonymous
Not applicable

Looking to further understand how letters and numbers can converge based on variables. I have (2) codes. One simply I want to type "ABC" and then get the real numbers to print "123" hence A=1 B=2 C=3. For now it only prints "abc" as typed and not the desired 123. I know that it's basically printing my variable I already typed but I though overriding this variable with the initial setq would fix this?On the contrary I want a second version that reverses this so as to type "123" and get it to print "ABC". I cannot seem to get them to work I think I am misunderstanding something.

(defun c:input (/ n1 n2)
(setq n1 '((a 1)(b 2)(c 3)))
(setq n2 (getstring "Enter Letters: "))
(setq n3 (atoi n2 n1))
(princ n3)
)
(defun c:output (/ n1 n2)
(setq n1 '((1 a)(2 b)(3 c)))
(setq n2 (initget 7 "Enter Numbers: "))
(setq n3 (itoa n2 n1))
(princ n3)
)
0 Likes
Accepted solutions (1)
4,795 Views
21 Replies
Replies (21)
Message 2 of 22

ВeekeeCZ
Consultant
Consultant

Here's the first one... I guess the second is up to you!?

 

(defun c:input (/ n1 n2)
  (setq n1 '(("a" 1)("b" 2)("c" 3)))
  (setq n2 (getstring "Enter Letters: "))
  (setq n3 (cadr (assoc (strcase n2 t) n1)))
  (princ n3)
  (princ)
  )

 

Some hints for the second.

(initget 7) stands alone prior to (get*) functions.

(getstring) returns string as "a" or "1"

(getint) returns an integer as 1. Text is not allowed.

(itoa) converts an integer into string: (itoa 1) returs "1".

0 Likes
Message 3 of 22

pbejse
Mentor
Mentor

Did you mean "a", "b" or "c" ? To get 1, 2 or 3?

Or you really want to type "abc" to get "123" or say "ac" gets you "13" ?

0 Likes
Message 4 of 22

Kent1Cooper
Consultant
Consultant

I agree with @pbejse's questions, and @ВeekeeCZ's pointers, if not with the code in Message 2, which would appear to work only for single-letter input.  Multi-letter input could certainly be done, but it would require stepping through the input string one character at a time.

 

Also, would this concept extend beyond just a few letters at the beginning of the alphabet?  To get "KLM" as an output, you would need to type 11 & 12 & 13, presumably in the format "111213", but in that case how is it to know that you don't want the result to be "AAABAC"?

Kent Cooper, AIA
0 Likes
Message 5 of 22

Anonymous
Not applicable

Yes I need the numbers/letters to be arbitrary in sequence. If CAB was typed the display would be 312. Sorry for not making this clearer.

0 Likes
Message 6 of 22

Anonymous
Not applicable

Essentially for the start of this imagine the alphabet is numerically represented. Numbers 1-27 would be A-Z. So in this case I want to type "85-12-12-15" to get "hello" and vice versa. For whatever reason I wasn't thinking passed single digit numbers (was just starting basic) and it didn't occur to me that something like "26" could be read as "bf" or just "y". So I guess I'm also looking for a way to insert the hyphens or some other separator. Maybe some condition that if the number is 9 or below read the numbers separate but above that use as a single number what's between hyphens?

0 Likes
Message 7 of 22

Anonymous
Not applicable

Wow this got complicated fast. Right after asking I realized how annoying the hyphen solution would be. This is way above what I can think of how to code this. Essentially I would just want to type "85121215" and get "hello". I'm thinking that somehow the lisp will have to read the first 2 digits for start. It will read "8" and "5". It will take these two numbers, combine them on the surface (to get "85" rather than 13), if the number is greater than 27 then read as individual numbers only. So "8" and "5" would be stored and then the next two would be evaluated. "12" would be read as "L" since the two numbers are less than 27 when combined on the "surface". Same for the others. I don't know if this can be done.

0 Likes
Message 8 of 22

ВeekeeCZ
Consultant
Consultant

You might use + as a delimiter. You need to have SOME delimiter. If you use a numpad, it's quite natural...

85+12+12+15

0 Likes
Message 9 of 22

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

.... I would just want to type "85121215" and get "hello". ... somehow the lisp will have to read the first 2 digits for start. It will read "8" and "5". It will take these two numbers ... if the number is greater than 27 then read as individual numbers only. So "8" and "5" would be stored and then the next two would be evaluated. "12" would be read as "L" ....


That's not going to be unambiguous.  "85121215" could be "heababae".  What if you want [whether at the start or elsewhere] "ab", not "l"?  Either would be "12".  For another example, "head" and "hen" would both be "8514" -- how could it know which you intended?

 

I would suggest you consider using two digits for each, with preceding zeros for the one-digit numbers:  01 for "a", 02 for "b", etc.  Then you wouldn't need to have separators/delimiters, and there would never be ambiguity.

 

But then there's the question of upper-case vs. lower-case letters....

Kent Cooper, AIA
Message 10 of 22

Anonymous
Not applicable

That works great for me! No need to clarify upper and lower case they can all just be upper!

0 Likes
Message 11 of 22

pbejse
Mentor
Mentor

I suppose there are no numbers or symbols as well? " 2 EGGS AT 5$?" 

0 Likes
Message 12 of 22

Sea-Haven
Mentor
Mentor

I dont understand so many posts and nobody looked at just use the ascii code returned for the letters A=65 a=97 as mentioned need to walk trough the string 1 character at a time so ABC-def is valid also just strcase 1st so no lowercase. 

 

(defun c:numstr ( / str x num)			 
(setq str (strcase (getstring "\nType in string")))
(setq x 1 newstr "")
(repeat (strlen str)
    (setq num  (- (ascii (substr str x 1)) 64))
    (if (and (>  num 0) (<  num 27))
      (progn
        (setq   newstr (strcat newstr (rtos num 2 0)))
        (setq x (+ x 1))
      )
    )
)
(princ)
)

 

Need some real strings to look at combos as result ABcde-FG

 

0 Likes
Message 13 of 22

pbejse
Mentor
Mentor

@Anonymous wrote:

..Essentially I would just want to type "85121215" and get "hello". .


Is this some sort of encrypting/decypting code? Did anyone approach you to crack the enigma machine ? 😄 

If that's the case,  you may also need a key to go with the message. Please tell us more.

 

Just so you know,  I went through the same phase when i was starting to learn how to code

0 Likes
Message 14 of 22

Kent1Cooper
Consultant
Consultant

@Sea-Haven wrote:

.... just use the ascii code returned for the letters A=65 a=97 ....


That's valid when going from letters to numbers, since letters are only one character each.  But going from numbers to letters still has the problem that not all have the same number of digits.  It could work if limited to capital letters and if Users know the appropriate numbers from 65 up and can enter them that way, so that all can be 2-digit numbers.  But if you want 1 to represent A, etc., the same issues arise.

Kent Cooper, AIA
0 Likes
Message 15 of 22

jlbelshan
Explorer
Explorer

The switch from ASCII to base-1 would just mean having the code add 64 or 96 to user input or subtract it from ASCII before making the number string.  Agree that padding numbers to 2 digits is necessary. Whether 2-digit numbers are required at entry depends on whether code takes a string of numbers (padding reqd) or whether the user is typing them one at a time with a delimiter like SPACE or ENTER between them (no padding reqd).  Also, the offset from ASCII to 1 would break if the code is supposed to handle digits because they’d be negative after subtracting 64 or 96, so you’d need to screen for valid inputs in any case.  

0 Likes
Message 16 of 22

Anonymous
Not applicable

Haha I see where you are coming from. Yes I am trying to make a very basic algorithm lisp. I've got people that work with me that understand CAD and lisps and don't get algorithms. So I was trying to make a simple one to show how it all works and I couldn't figure it out so here I am. I know it's possible. Read some guy made an insanely tough algorithm all in lisp. It's a language called "Malboge". Just saying "hello world" is like two paragraphs long in that. I just want my simple letter to number algorithm example lol.

0 Likes
Message 17 of 22

Sea-Haven
Mentor
Mentor

Kent yes your right I did some code at some point in time and you reminded me needed to be 2 digit answer so A=01. The lower case can be incorporated then also.

 

HELLO = 0804111114 

0 Likes
Message 18 of 22

pbejse
Mentor
Mentor
Accepted solution

@Anonymous wrote:

 I just want my simple letter to number algorithm example lol.


Okay then, now lets play with a simple algorhythm allgorithem  algorithm.

You can start by creating a list using direct substitution. 

 

(defun c:input (/ n1 n2 n2l)
(setq n1 '((69 . "05") (72 . "08")(76 . "12")(79 . "15")) )
(if
  (and
     (setq n2 (strcase (getstring  "\nEnter Letters: ")))
     (vl-every '(lambda (l)
		     (assoc l n1))
		     (setq n2l (vl-string->list n2))
		  )
     )
  (print (apply 'strcat
	       (mapcar '(lambda	(c)
			  (cdr (assoc c n1))
			)
			n2l
		       )
	       )
	)
  (princ "\nInvalid character input")
  )(princ)
  )

 

Command: Input

Enter Letters: Hello

"0805121215"

Or you can use any single character and not necessarily a number to make it easier to decrpyt by replacing n1 list with this

 

(setq n1 '((69 . "^")(72 . "Z")(76 . "3")(79 . "$")))

 

Command: Input

Enter Letters: Hello

Z^33$

Which in turn returning it back to "HELLO" is easy using the same code and replacing these two lines

 

(defun c:output (/ n1 n2 n2l)
(setq n1 '(( 94 . "E")(90 . "H")(51 . "L")(36 . "O")))
...	
(setq n2 (strcase (getstring "\nEnter string to decrypt: ")))
...

 

 

Command: output

Enter string to decrypt:  Z^33$

"HELLO"

You can build the entire substition list to include just about everything.

 

HTH

 

Message 19 of 22

Anonymous
Not applicable

That is awesome man thank you so much! I will definitely be messing with this a lot and sharing it. Much appreciated, seriously. Everyone will love this.

0 Likes
Message 20 of 22

pbejse
Mentor
Mentor

@Anonymous wrote:

That is awesome man thank you so much! I will definitely be messing with this a lot and sharing it. Much appreciated, seriously. Everyone will love this.


 

Anytime LispHunt42, let us know if you need more help with coding a more complex algorithm.

Cheers

 

0 Likes