473,899 Members | 3,071 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
80 5364
On 20 May 2006 14:45:05 -0700, I waved a wand and this message
magically appeared from Gaijinco:
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;
}

I'd do something like this:

short char2digit(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
}

--
http://www.munted.org.uk

Take a nap, it saves lives.
May 21 '06 #21
Alex Buell wrote:
On 20 May 2006 14:45:05 -0700, I waved a wand and this message
magically appeared from Gaijinco:

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;
}

I'd do something like this:

short char2digit(char c)
{
if (c >= '0' && c <= '9')
return c - '0';

else ? }

--
Ian Collins.
May 21 '06 #22
Alex Buell <al********@mun ted.org.uk> writes:
On Sun, 21 May 2006 17:15:21 +1200, I waved a wand and this message
magically appeared from Ian Collins:
> I'd do something like this:
>
> short char2digit(char c)
> {
> if (c >= '0' && c <= '9')
> return c - '0';

else ?
> }
>

Oh yes. Don't need an else, but I'd put in a return -1 just in case
input isn't valid.

Yes, you need an else. If you're going to assume the input is valid,
you don't need the if. If you're not going to assume the input is
valid, you need the if *and* the else.

--
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 #23
On Sun, 21 May 2006 05:46:33 GMT, I waved a wand and this message
magically appeared from Keith Thompson:
Oh yes. Don't need an else, but I'd put in a return -1 just in case
input isn't valid.

Yes, you need an else. If you're going to assume the input is valid,
you don't need the if. If you're not going to assume the input is
valid, you need the if *and* the else.

Eh? Are you saying the below won't work? It should return -1 for values
not in the 0..9 range.

short char2digt(char c)
{
if (c => '0' && c <= '9)
return c - '0';

return -1;
}

--
http://www.munted.org.uk

Take a nap, it saves lives.
May 21 '06 #24
Alex Buell wrote:

On 20 May 2006 14:45:05 -0700, I waved a wand and this message
magically appeared from Gaijinco:
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;
}

I'd do something like this:

short char2digit(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
}

The meaning of the code is simpler
if you use type int for your parameter and return type,
otherwise you have implicit conversions all over the place.
The number of standard library functions which use
either char or short, for parameter type or return type,
is zero.

--
pete
May 21 '06 #25
On Sun, 21 May 2006 06:03:32 GMT, I waved a wand and this message
magically appeared from pete:
short char2digit(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
}

The meaning of the code is simpler
if you use type int for your parameter and return type,
otherwise you have implicit conversions all over the place.
The number of standard library functions which use
either char or short, for parameter type or return type,
is zero.

Yes I know but I wasn't the OP ;o)

--
http://www.munted.org.uk

Take a nap, it saves lives.
May 21 '06 #26
Alex Buell <al********@mun ted.org.uk> writes:
On Sun, 21 May 2006 05:46:33 GMT, I waved a wand and this message
magically appeared from Keith Thompson:
> Oh yes. Don't need an else, but I'd put in a return -1 just in case
> input isn't valid.

Yes, you need an else. If you're going to assume the input is valid,
you don't need the if. If you're not going to assume the input is
valid, you need the if *and* the else.

Eh? Are you saying the below won't work? It should return -1 for values
not in the 0..9 range.

short char2digt(char c)
{
if (c => '0' && c <= '9)
return c - '0';

return -1;
}

No, I just misunderstood. When you said "Don't need an else", I
thought you meant leaving the code as it was originally (which falls
off the end of the function if the argument is out of range).

I'd probably put the "return -1;" in an else clause, but that's just a
matter of style.

I'd also have the function return int rather than short.

--
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 #27
Keith Thompson wrote:

Alex Buell <al********@mun ted.org.uk> writes:
On Sun, 21 May 2006 05:46:33 GMT, I waved a wand and this message
magically appeared from Keith Thompson:
> Oh yes. Don't need an else, but I'd put in a return -1 just in case
> input isn't valid.

Yes, you need an else. If you're going to assume the input is valid,
you don't need the if. If you're not going to assume the input is
valid, you need the if *and* the else.

Eh? Are you saying the below won't work? It should return -1 for values
not in the 0..9 range.

short char2digt(char c)
{
if (c => '0' && c <= '9)
return c - '0';

return -1;
}

No, I just misunderstood. When you said "Don't need an else", I
thought you meant leaving the code as it was originally (which falls
off the end of the function if the argument is out of range).

I'd probably put the "return -1;" in an else clause, but that's just a
matter of style.

I'd also have the function return int rather than short.

int char2digt(int c)
{
return c => '0' && c <= '9 ? c - '0' : -1;
}

--
pete
May 21 '06 #28
Keith Thompson said:
Martin Ambuhl <ma*****@earthl ink.net> 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;
return d;
}
?

#define CHAR2DIGIT(c) ((c)-'0')
#define DIGIT2CHAR(d) ((d)+'0')

I don't think I'd bother with the macros. Just using c-'0' or d+'0'
seems clear enough to me. YMMV.

In fact, this is a case where the code is actually clearer than the macro
names that purport to represent it. Why, for example, is there a number 2
in those names?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 21 '06 #29
On Sun, 21 May 2006 06:24:40 GMT, I waved a wand and this message
magically appeared from pete:
I'd also have the function return int rather than short.

int char2digt(int c)
{
return c => '0' && c <= '9 ? c - '0' : -1;
}

Now that's I like to see!
--
http://www.munted.org.uk

Take a nap, it saves lives.
May 21 '06 #30

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