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

Convert a non-digit char to int

p_vp_v 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

Comments

  • stoberstober 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/
  • tsagldtsagld 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


Sign In or Register to comment.