How to convert an ASCII HEX character to it's value (0-15)?

Posted by philcolbourn on Stack Overflow See other posts from Stack Overflow or by philcolbourn
Published on 2010-03-23T10:18:37Z Indexed on 2010/03/23 10:23 UTC
Read the original article Hit count: 325

Filed under:
|
|

I am writing a string parser and the thought occurred to me that there might be some really interesting ways to convert an ASCII hexadecimal character [0-9A-Fa-f] to it's numeric value.

What are the quickest, shortest, most elegant or most obscure ways to convert [0-9A-Fa-f] to it's value between 0 and 15?

Assume, if you like, that the character is a valid hex character.

I have no chance so I'll have a go at the most boring.

( c <= '9' ) ? ( c - '0' ) : ( (c | '\x60') - 'a' + 10 )

© Stack Overflow or respective owner

Related posts about ascii

Related posts about hex