1. Convert ASCII code to equivalent numerical value

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?

new topic     » topic index » view message » categorize

2. Re: Convert ASCII code to equivalent numerical value

alrobnett said...

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.

new topic     » goto parent     » topic index » view message » categorize

3. Re: Convert ASCII code to equivalent numerical value

Thanks, Igregg, Just what I need, and better than I had hoped for.

new topic     » goto parent     » topic index » view message » categorize

4. Re: Convert ASCII code to equivalent numerical value

alrobnett said...

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' 
new topic     » goto parent     » topic index » view message » categorize

5. Re: Convert ASCII code to equivalent numerical value

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.

new topic     » goto parent     » topic index » view message » categorize

6. Re: Convert ASCII code to equivalent numerical value

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

new topic     » goto parent     » topic index » view message » categorize

7. Re: Convert ASCII code to equivalent numerical value

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.

new topic     » goto parent     » topic index » view message » categorize

8. Re: Convert ASCII code to equivalent numerical value

alrobnett said...

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 :)

new topic     » goto parent     » topic index » view message » categorize

9. Re: Convert ASCII code to equivalent numerical value

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.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu