Convert a non-digit char to int - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# Convert a non-digit char to int

Posts: 61Member
Hi,

While browsing the code for my project, I found the following snippet:
if(isdigit(chararray[0]))
val = atoi(chararray);
else
val = chararray[0] - 'a' + 10;

I am not sure I understand the code under the else part.

Can anyone throw some light?

TIA
PV

• Posts: 9,765Member ✭✭✭
every character on the keyboard has an ascii value (google for "ascii chart") and there are 256 possible values. For example, the letter 'a' has an ascii value of 97. If chararray[0] == 'a', then the formula you posted will yield 97 - 97 + 10 which is just 10. If chararray[0] == '#' then it will yield 35 - 97 + 10 which is -52.

http://www.lookuptables.com/
• Posts: 621Member
: every character on the keyboard has an ascii value (google for "ascii chart") and there are 256 possible values. For example, the letter 'a' has an ascii value of 97. If chararray[0] == 'a', then the formula you posted will yield 97 - 97 + 10 which is just 10. If chararray[0] == '#' then it will yield 35 - 97 + 10 which is -52.
:
: http://www.lookuptables.com/
:
It looks like a code snippet to convert a hexadecimal digit to decimal.

Greets,
Eric Goldstein
http://www.gvh-maatwerk.nl