1. Convert ASCII code to equivalent numerical value
- Posted by alrobnett Jan 15, 2019
- 1097 views
given:
integer key
key = wait_key
If the key pressed is 1, then the value in key = 49.
I need a value of 1.
I could say:
key -= 48
Is there a more sanitary way?
2. Re: Convert ASCII code to equivalent numerical value
- Posted by lgregg Jan 15, 2019
- 1138 views
given:
integer key
key = wait_key
If the key pressed is 1, then the value in key = 49.
I need a value of 1.
I could say:
key -= 48
Is there a more sanitary way?
Take a look in the manual for the following:
8.3.4.7 prompt_number
include std/console.e
namespace console
public function prompt_number(sequence prompt, sequence range)
Prompts the user to enter a number, and returns only validated input.
Parameters:
st : is a string of text that will be displayed on the screen. (should be prompt)
s : is a sequence of two values {lower, upper} which determine the range of values that the user may enter. s can be empty, {}, if there are no restrictions. (should be range)
Returns:
An atom, in the assigned range which the user typed in.
3. Re: Convert ASCII code to equivalent numerical value
- Posted by alrobnett Jan 15, 2019
- 1132 views
Thanks, Igregg, Just what I need, and better than I had hoped for.
4. Re: Convert ASCII code to equivalent numerical value
- Posted by petelomax Jan 16, 2019
- 1079 views
key -= 48 Is there a more sanitary way?
For the single-character case, for the love of god please, instead of that do this:
key -= '0'
5. Re: Convert ASCII code to equivalent numerical value
- Posted by alrobnett Jan 16, 2019
- 1039 views
Sorry Pete, but I have not been able to find out what you mean.
key -= 48
may be wrong, but it worked very well.
prompt_number()
is a big improvement, but the love of god did not help me understand
key -= '0'
In my case, would that have been
key -= '48'
If so, the purpose of the single quote is not obvious to me.
6. Re: Convert ASCII code to equivalent numerical value
- Posted by ChrisB (moderator) Jan 16, 2019
- 1043 views
Hi
'0' is a character, and looks to the interpreter like 48, so
48 + 2 = 50
'0' + 2 = 50
To the interpreter, it's the same thing
similarly
'A' + 3 = 'C'
try
puts(1, 'A' + 3)
Cheers
Chris
7. Re: Convert ASCII code to equivalent numerical value
- Posted by alrobnett Jan 16, 2019
- 1047 views
Thanks, Chris and Pete. I finally found the pertinent info in the manual by searching for "character". Interestingly, the manual states that the single quotes make it possible to use the character without having to know the ASCII code. I went the other way around, which seems clearer. It is not obvious to me why god has a preference.
8. Re: Convert ASCII code to equivalent numerical value
- Posted by irv Jan 16, 2019
- 1031 views
Thanks, Chris and Pete. I finally found the pertinent info in the manual by searching for "character". Interestingly, the manual states that the single quotes make it possible to use the character without having to know the ASCII code. I went the other way around, which seems clearer. It is not obvious to me why god has a preference.
Neither He nor the interpreter care.
This does, in some small way, help you when you later look at your code. Using the single quotes around a character indicates to you that you meant to deal with characters, not numbers, per se.
And, of course, if they ever decide to change the ASCII codes... well, you'd be prepared :)
9. Re: Convert ASCII code to equivalent numerical value
- Posted by petelomax Jan 16, 2019
- 1069 views
God invented '0' at the beginning of time, or maybe it was the Arabs 10,000 years ago, and everyone knows what it is.
Some faceless bureaucrat on the ASCII committee invented 48 sometime in the 1970s, and no-one knows and no-one cares.
My point was, as others have said and I think you now know, '0' states the intent precisely, whereas 48, while equivalent, is just deliberately cryptic.