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

P

pete

Richard said:
Martin Jørgensen said:

I count five. Is this a case of one, two, three, er, many?

4 arithmetic operations and one relational operation.
I consider the relational operation to be more like a decission,
like an if statement or a conditional operator,
than like a calculation.
 
C

CBFalconer

Richard said:
Peter Jansson said:

Gladly. Instead of writing:

n = char_to_int(ch);

write:

n = ch - '0';

And indeed that's what I do in my own code. It may be objected
that recent versions of char_to_int have range checks, and return
-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.com, 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
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
D

Dale Henderson

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.
 
R

Richard Bos

pete said:

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
 
P

pete

Richard said:
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.
 
R

Richard Bos

pete said:
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
 
P

pete

Richard said:
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.
 
P

pete

pete said:
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,
I was asked about what was converted "to" what.
 
R

Robert Latest

["Followup-To:" header set to comp.lang.c.]
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
 
T

tedu

Why is the first macro called CHAR-zwei-DIGIT?

maybe he thought it sounded better than BUCHSTABEZWEISTELLE.

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

Duane Hebert

tedu said:
maybe he thought it sounded better than BUCHSTABEZWEISTELLE.

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

If you want to chart an odigit. Maybe char_to_digit or charToDigit or
something...
 
V

Victor Bazarov

Duane said:
If you want to chart an odigit. Maybe char_to_digit or charToDigit or
something...

For those who don't know English, does 'char' or 'digit' mean the same
thing as for those who do know it? Then would it be possible that when
they read the name, instead of thinking 'zwei', they do think 'two',
simply because they are in the "English" mode? Just a thought...
 
A

Alex Buell

For those who don't know English, does 'char' or 'digit' mean the same
thing as for those who do know it? Then would it be possible that
when they read the name, instead of thinking 'zwei', they do think
'two', simply because they are in the "English" mode? Just a
thought...

In English, my fingers are also 'digits'.
 
W

Walter Bright

Alf said:
In C++ it's no big deal to define a non-macro conversion that yields a
compile time constant:

template< char digit >
struct IntFromDigit { enum{ value = digit - '\0' }; };

...
int array[IntFromDigit<'7'>::value];

If that conversion were ever required, it would be in template code, so
a template solution is very fitting.

Just use:

(digit - '0')

and be done with it.
 
V

Vladimir Oka

Victor Bazarov opined:
For those who don't know English, does 'char' or 'digit' mean the
same thing as for those who do know it?

For those who don't know English, both mean absolutely nothing at all
(unless they speak Latin, or their native language is of Romance
descent, in which case they may get the "digit").
Then would it be possible that when they read the name, instead of
thinking 'zwei', they do think 'two', simply because they are in the
"English" mode? Just a thought...

Using "2" for "to" became commonplace in software circles, so I doubt
it's misleading. OTH, I still mentally read out all the numbers (apart
from char2digit variety) in my native language, even in English mode
(and I'm in English mode for at least 75% of my waking hours). YMMV.
 
A

Alf P. Steinbach

* Walter Bright:
Alf said:
In C++ it's no big deal to define a non-macro conversion that yields a
compile time constant:

template< char digit >
struct IntFromDigit { enum{ value = digit - '\0' }; };

...
int array[IntFromDigit<'7'>::value];

If that conversion were ever required, it would be in template code,
so a template solution is very fitting.

Just use:

(digit - '0')

and be done with it.

What I wrote was, just use the number 7. ;-)
 
V

Vladimir Oka

Vladimir Oka opined:
Victor Bazarov opined:


For those who don't know English, both mean absolutely nothing at all
(unless they speak Latin, or their native language is of Romance
descent, in which case they may get the "digit").

Not to mention that, out of context, "char" can be construed as a
variant of "charcoal", especially if, in a different life, one was
involved in coal combustion. ;-)
 
V

Victor Bazarov

Vladimir said:
Vladimir Oka opined:


Not to mention that, out of context, "char" can be construed as a
variant of "charcoal", especially if, in a different life, one was
involved in coal combustion. ;-)

Yeah, yeah... And "char2digit" means "Zwei parts ashes, ein part finger"
(analogous to H2O), right?
 
C

crichmon

Gaijinco said:
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.



int char_to_digit(const char & chardigit)
{
static const int zeroindex = static_cast<int>('0');

int digit = (static_cast<int>(chardigit) - zeroindex);

if ((digit > 9) || (digit < 0)) return -1; // or throw an exception

return digit;
};
 
C

crichmon

oops, rather

short char_to_digit(const char & chardigit)
{
static const short zeroindex = static_cast<short>('0');

short digit = (static_cast<short>(chardigit) - zeroindex);

if ((digit > 9) || (digit < 0)) return -1; // or throw an exception
else return digit;
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,436
Messages
2,571,696
Members
48,796
Latest member
Greg L.
Top