473,900 Members | 4,105 Online

# char [0-9] to int [0-9]

Is there an elegant way to do something like

// given a char {0,1,2,3,4,5,6, 7,8,9} returns it as a short
short char2digit(char c)
{
short d = c-48;
return d;
}

?

Thanks.

May 21 '06 #1
80 5365

Gaijinco wrote:
Is there an elegant way to do something like

// given a char {0,1,2,3,4,5,6, 7,8,9} returns it as a short
short char2digit(char c)
{
short d = c-48;
d = c - '0';

is the portable solution.
return d;
}

Tobias

May 21 '06 #2
Gaijinco wrote:
Is there an elegant way to do something like

// given a char {0,1,2,3,4,5,6, 7,8,9}
I assume you mean '0' etc. The char type is an integer type and could
actually be set to those integral values. The '' are used with
character literals to differentiate that.
returns it as a short
short char2digit(char c)
{
short d = c-48;
return d;
}

?

That's not portable, as it assumes the ASCII character set. The way to
do it is:

short char2digit(char c)
{
return c-'0';
}
Your variable d was unneeded, but not harmful.

Brian
May 21 '06 #3
to*******@hotma il.com writes:
Gaijinco wrote:
Is there an elegant way to do something like

// given a char {0,1,2,3,4,5,6, 7,8,9} returns it as a short
short char2digit(char c)
{
short d = c-48;

d = c - '0';

is the portable solution.

Yes, and this true only because the standard specifically guarantees
that the digits '0' through '9' have representations that are
consecutive and in increasing order. Keep in mind that there is no
such guarantee for other characters, such as upper or lower case
letters (and there are character sets in which the letters are not
contiguous).

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 21 '06 #4
Gaijinco wrote:
Is there an elegant way to do something like

// given a char {0,1,2,3,4,5,6, 7,8,9} returns it as a short
short char2digit(char c)
{
short d = c-48;
return d;
}

the range of c!

--
Ian Collins.
May 21 '06 #5
Keith Thompson wrote:
to*******@hotma il.com writes:
Gaijinco wrote:
Is there an elegant way to do something like

// given a char {0,1,2,3,4,5,6, 7,8,9} returns it as a short
short char2digit(char c)
{
short d = c-48;

d = c - '0';

is the portable solution.

Yes, and this true only because the standard specifically guarantees
that the digits '0' through '9' have representations that are
consecutive and in increasing order. Keep in mind that there is no
such guarantee for other characters, such as upper or lower case
letters (and there are character sets in which the letters are not
contiguous).

What if there was a system where the digits '0' through '9' is not
continuous? Would the standard translate them to a continuous
representation?

/Michael
May 21 '06 #6
Op Sat, 20 May 2006 22:20:04 GMT schreef Michael Brennan:
Keith Thompson wrote:
to*******@hotma il.com writes:
Gaijinco wrote:
Is there an elegant way to do something like

// given a char {0,1,2,3,4,5,6, 7,8,9} returns it as a short
short char2digit(char c)
{
short d = c-48;
d = c - '0';

is the portable solution.

Yes, and this true only because the standard specifically guarantees
that the digits '0' through '9' have representations that are
consecutive and in increasing order. Keep in mind that there is no
such guarantee for other characters, such as upper or lower case
letters (and there are character sets in which the letters are not
contiguous).

What if there was a system where the digits '0' through '9' is not
continuous? Would the standard translate them to a continuous
representation?

/Michael

When you still use a Telex keyboard to program in C, you could be in
trouble ;-)
--
Coos
May 21 '06 #7
Michael Brennan wrote:
What if there was a system where the digits '0' through '9' is not
continuous? Would the standard translate them to a continuous
representation?

A conforming C implementation has to do whatever it takes
to make those characters appear to have consective values
in a C program.
In other words, a C programmer can assume that
they are consecutive and in order.

('9' - '0') is 9, and you can rely on that in a C program.

--
pete
May 21 '06 #8
Michael Brennan <br************ @gmail.com> writes:
Keith Thompson wrote:
to*******@hotma il.com writes:
Gaijinco wrote:
Is there an elegant way to do something like

// given a char {0,1,2,3,4,5,6, 7,8,9} returns it as a short
short char2digit(char c)
{
short d = c-48;
d = c - '0';

is the portable solution.

Yes, and this true only because the standard specifically guarantees
that the digits '0' through '9' have representations that are
consecutive and in increasing order. Keep in mind that there is no
such guarantee for other characters, such as upper or lower case
letters (and there are character sets in which the letters are not
contiguous).

What if there was a system where the digits '0' through '9' is not
continuous? Would the standard translate them to a continuous
representation?

I suppose it would, but I don't think there are any character sets
currently in use in which the digits aren't contiguous.

The only current character sets I know of are EBCDIC, ASCII, and
various ASCII derivatives (including 8859-* and Unicode).

In EBCDIC, the letters aren't contiguous; if they had been, it's
possible that C would have required that as well.

Believe it nor not, some of C's requirements are actually based on
reality. 8-)}

(I just noticed this is cross-posted to comp.lang.c++. I presume C++
has the same rules in this area as C, but I'm not certain.
Cross-posts to comp.lang.c and comp.lang.c++ are usually a bad idea.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 21 '06 #9
Gaijinco wrote:
Is there an elegant way to do something like

// given a char {0,1,2,3,4,5,6, 7,8,9} returns it as a short
short char2digit(char c)
{
short d = c-48;
return d;
}

?

#define CHAR2DIGIT(c) ((c)-'0')
#define DIGIT2CHAR(d) ((d)+'0')
May 21 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.