473,899 Members | 2,918 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 5365
On Sun, 21 May 2006 15:31:06 GMT, I waved a wand and this message
magically appeared from Keith Thompson:
int char2digit(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
else
return -1;
}

Why bother? There's a much better solution that someone has already
posted. It uses isdigit()
--
http://www.munted.org.uk

Take a nap, it saves lives.
May 21 '06 #61
>>>>> "RH" == Richard Heathfield <in*****@invali d.invalid> writes:
I would never choose that solution...

RH> Neither would I. The consensus appears to be that the whole
RH> function is a complete waste of time.

I never intended it as a real solution.

RH> But Dale's code is still neat. :-)

Take some credit, I only debuged your code.
May 22 '06 #62
Richard Heathfield wrote:
Peter Jansson said:
Richard Heathfield wrote:

Er, yes, although I'd probably want to find a way to get rid of
the abs.

Come to think of it, I can see a really easy way to get rid of
the whole function. :-)

Would you like to share that "easy way" with us?

n = char_to_int(ch) ;

write:

n = ch - '0';

And indeed that's what I do in my own code. It may be objected
-1 if an out-of-range value is passed. But this means that you
have to do something like: ... snip ...

Provided n and ch are unsigned, then the test is simple:

if ((n = ch - '0') > 9) barfloudly();

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
May 22 '06 #63
pete <pf*****@mindsp ring.com> wrote:
Richard Heathfield wrote:

Keith Thompson said:
Martin Ambuhl <ma*****@earthl ink.net> writes: #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?

atoi
a2i

What does a-twee-i have to do with atoi? Why is the first macro called
CHAR-zwei-DIGIT? Why is the second called DIGIT-deux-CHAR?

Richard
May 22 '06 #64
Richard Bos wrote:

pete <pf*****@mindsp ring.com> wrote:
Richard Heathfield wrote:

Keith Thompson said:

> Martin Ambuhl <ma*****@earthl ink.net> writes:

>> #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?

atoi
a2i

What does a-twee-i have to do with atoi? Why is the first macro called
CHAR-zwei-DIGIT? Why is the second called DIGIT-deux-CHAR?

I don't know.

I don't take languages that I don't know,
into consideration, when I write.
I'm American,
so I only know some of the American dialects of English.

--
pete
May 22 '06 #65
pete <pf*****@mindsp ring.com> wrote:
Richard Bos wrote:

pete <pf*****@mindsp ring.com> wrote:
Richard Heathfield wrote:
>
> Keith Thompson said:
>
> > Martin Ambuhl <ma*****@earthl ink.net> writes:

> >> #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?

atoi
a2i

What does a-twee-i have to do with atoi? Why is the first macro called
CHAR-zwei-DIGIT? Why is the second called DIGIT-deux-CHAR?

I don't know.

I don't take languages that I don't know,
into consideration, when I write.
I'm American,
so I only know some of the American dialects of English.

Ok, let's try again, for the nil-lingual:

What happened to CHAR1DIGIT? Was it an inferior version? Will we get a
DIGIT3CHAR at some point?

Richard
May 22 '06 #66
Richard Bos wrote:

pete <pf*****@mindsp ring.com> wrote:
Richard Bos wrote:

pete <pf*****@mindsp ring.com> wrote:

> Richard Heathfield wrote:
> >
> > Keith Thompson said:
> >
> > > Martin Ambuhl <ma*****@earthl ink.net> writes:
>
> > >> #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?
>
> atoi
> a2i

What does a-twee-i have to do with atoi?
Why is the first macro called
CHAR-zwei-DIGIT? Why is the second called DIGIT-deux-CHAR?

I don't know.

I don't take languages that I don't know,
into consideration, when I write.
I'm American,
so I only know some of the American dialects of English.

Ok, let's try again, for the nil-lingual:

What happened to CHAR1DIGIT?

If there was a way that a char could win a digit,
and I wanted to determine if that had happened,
I might write CHAR1DIGIT instead of CHARWONDIGIT.

--
pete
May 22 '06 #67
pete wrote:

Richard Bos wrote:

pete <pf*****@mindsp ring.com> wrote:
Richard Bos wrote:
>
> pete <pf*****@mindsp ring.com> wrote:
>
> > Richard Heathfield wrote:
> > >
> > > Keith Thompson said:
> > >
> > > > Martin Ambuhl <ma*****@earthl ink.net> writes:
> >
> > > >> #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?
> >
> > atoi
> > a2i
>
> What does a-twee-i have to do with atoi?
> Why is the first macro called
> CHAR-zwei-DIGIT? Why is the second called DIGIT-deux-CHAR?

I don't know.

I don't take languages that I don't know,
into consideration, when I write.
I'm American,
so I only know some of the American dialects of English.

Ok, let's try again, for the nil-lingual:

What happened to CHAR1DIGIT?

If there was a way that a char could win a digit,
and I wanted to determine if that had happened,
I might write CHAR1DIGIT instead of CHARWONDIGIT.

I think now I'm getting a little silly.

My point is that I've seen Martin Ambuhl's
naming convention in code before, and I've used it,
and when I was asked what my list_x2x function did,

--
pete
May 22 '06 #68
What if there was a system where the digits '0' through '9' is not
continuous? Would the standard translate them to a continuous
representation?

This is what the Standard has to say about the topic:

In both the source and execution basic character sets,
the value of each character after 0 in the above list of
decimal digits shall be one greater than the value of
the previous.

What I don't understand is why the consecutivity of 0 thru 9 is
required from the *source* character set as well. In case source
and execution set differ, won't the compiler have to translate
all character constants and string literals anyway?

robert
May 22 '06 #69
Richard Bos wrote:
> Martin Ambuhl <ma*****@earthl ink.net> writes:
>> #define CHAR2DIGIT(c) ((c)-'0')
Why is the first macro called CHAR-zwei-DIGIT?

maybe he thought it sounded better than BUCHSTABEZWEIST ELLE.

if one doesn't know english, who would CHARTODIGIT be any more clear?

May 22 '06 #70

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