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

G

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

?

Thanks.
 
D

Default User

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

Keith Thompson

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).
 
I

Ian Collins

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;
}
In addition to the good advice already offered, don't forget to check
the range of c!
 
M

Michael Brennan

Keith said:
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
 
C

Coos Haak

Op Sat, 20 May 2006 22:20:04 GMT schreef Michael Brennan:
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 ;-)
 
P

pete

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

Keith Thompson

Michael Brennan said:
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. :cool:}

(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.)
 
M

Martin Ambuhl

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

?

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

Cy Edmunds

Martin Ambuhl said:
#define CHAR2DIGIT(c) ((c)-'0')
#define DIGIT2CHAR(d) ((d)+'0')

If you ever have a job interview and get asked this, don't use a macro where
an ordinary function will do. The interviewer might be me and I'd show you
the door in one second flat.

Cy
 
T

Tomás

Cy Edmunds posted:

If you ever have a job interview and get asked this, don't use a macro
where an ordinary function will do. The interviewer might be me and
I'd show you the door in one second flat.


At which point, I, the programmer, would propose that you allocate an array
whose length is determined by an ASCII digit character:

int array[ DigitToInt( '7' ) ];

ERROR: DigitToInt( '7' ) is not a compile-time constant.

Thus, the solution:

#define DigitToInt(...


If you don't like it, then either:

a) Change language
b) Learn to live with it
c) Goad the committee to change the Standard

-Tomás
 
R

Robert Gamble

Tomás said:
Cy Edmunds posted:

If you ever have a job interview and get asked this, don't use a macro
where an ordinary function will do. The interviewer might be me and
I'd show you the door in one second flat.


At which point, I, the programmer, would propose that you allocate an array
whose length is determined by an ASCII digit character:

int array[ DigitToInt( '7' ) ];

ERROR: DigitToInt( '7' ) is not a compile-time constant.

Thus, the solution:

#define DigitToInt(...

That's your justification for using a macro? You've got to be joking.
If you don't like it, then either:

a) Change language

They did, over 6 years ago. It's called variable length arrays.
b) Learn to live with it

I think I could live with having to type int array[7] instead of int
array[DigitToInt('7')]. Do you realize how silly your argument is?
c) Goad the committee to change the Standard

Again, already been done.

Robert Gamble
 
I

Ian Collins

Tomás said:
Cy Edmunds posted:


If you ever have a job interview and get asked this, don't use a macro
where an ordinary function will do. The interviewer might be me and
I'd show you the door in one second flat.



At which point, I, the programmer, would propose that you allocate an array
whose length is determined by an ASCII digit character:

int array[ DigitToInt( '7' ) ];
int array[7]; ?
 
A

Alf P. Steinbach

* Tomás:
Cy Edmunds posted:

If you ever have a job interview and get asked this, don't use a macro
where an ordinary function will do. The interviewer might be me and
I'd show you the door in one second flat.


At which point, I, the programmer, would propose that you allocate an array
whose length is determined by an ASCII digit character:

int array[ DigitToInt( '7' ) ];

ERROR: DigitToInt( '7' ) is not a compile-time constant.

Thus, the solution:

#define DigitToInt(...


If you don't like it, then either:

a) Change language
b) Learn to live with it
c) Goad the committee to change the Standard

Note that this thread has been cross-posted to [comp.lang.c] and
[comp.lang.c++] -- generally a trolling device.

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.

However, normally you'd just do

std::vector<int> array(intFromDigit('7'));

with 'intFromDigit' a normal function, or, better yet,

std::vector<int> array(7);

In C 99 you do not need a compile time constant to declare the array,
and so also in C 99 you can avoid a macro.

Anyway, please use ALL UPPERCASE names for macros (and for macros only),
to avoid macros changing the meaning of non-macro code.
 
J

Jerry Coffin

[ ... ]
If you ever have a job interview and get asked this, don't use a macro where
an ordinary function will do. The interviewer might be me and I'd show you
the door in one second flat.

It'd be your loss. It's one thing to worry about macros
that aren't safe (e.g. evaluate an argument more than
once) but these are entirely safe. To an extent, it'd
depend on the language though. In C++, I can't think of a
good reason to make these macros. In C, I can't think of
a good reason to make them functions.
 
C

Cy Edmunds

Tomás said:
Cy Edmunds posted:




At which point, I, the programmer, would propose

[snip]

Hold it right there. Once I show you the door you ain't proposing anything!
 
K

Keith Thompson

Martin Ambuhl said:
#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.
 
A

Alex Buell

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

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,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top