tags:

views:

129

answers:

3

Possible Duplicate:
Char to int conversion in C.

I remember learning in a course a long time ago that converting from an ASCII char to an int by subtracting '0' is bad.

For example:

int converted;
char ascii = '8';

converted = ascii - '0';

Why is this considered a bad practice? Is it because some systems don't use ASCII? The question has been bugging me for a long time.

+3  A: 

Edit: Apparently the C standard guarantees consecutive 0-9 digits.

ASCII is not guaranteed by the C standard, in effect making it non-portable. You should use a standard library function intended for conversion, such as atoi. However, if you wish to make assumptions about where you are running (for example, an embedded system where space is at a premium), then by all means use the subtraction method. Even on systems not in the US-ASCII code page (UTF-8, other code pages) this conversion will work. It will work on ebcdic (amazingly).

Yann Ramin
I don't atoi() would help here, since it would convert "3" to a 3, not to the ascii code. I also don't think it would convert "a" to its ASCII
Uri
atoi() won't work on a single character, it needs a null-terminated string.
Fred Larson
ascii is not guaranteed but the fact that the values of '0' to '9' are consecutive integers is.
Charles Bailey
A: 

This is a common trick taught in C classes primarily to illustrate the notion that a char is a number and that its value is different from the corresponding int.

Unfortunately, this educational toy somehow became part of the typical arsenal of most C developers, partially because C doesn't provide a convenient call for this (it is often platform specific, I'm not even sure what it is).

Generally, this code is not portable for non-ASCII platforms, and for future transitions to other encodings. It's also not really readable. At a minimum wrap this trick in a function.

Uri
It *is* portable to non-ASCII platforms because it is a language requirement that the values '0' to '9' are consecutive.
Charles Bailey
+7  A: 

While you probably shouldn't use this as part of a hand rolled strtol (that's what the standard library is for) there is nothing wrong with this technique for converting a single digit to its value. It's simple and clear, even idiomatic. You should, though, add range checking if you are not absolutely certain that the given char is in range.

It's a C language guarantee that this works.

5.2.1/3 says:

In both the source and execution basic character sets, the value of each character after 0 in the above list [includes the sequence: 0,1,2,3,4,5,6,7,8,9] shall be one greater that the value of the previous.

Character sets may exist where this isn't true but they can't be used as either source or execution character sets in any C implementation.

Charles Bailey