atoi question

R

Rick

Hi,

Another newbie question. I'm trying to convert a char to an integer. My
input is a string which holds a number on place 4. However, if I try
int number = atoi( input[3] );
I'll get an error because of the type conversion I guess. Atoi wants a
string while I put a single char into it. So I tried it another way:
char s[2];
int number;
s[0] = input[3];
s[1] = 0;
number = atoi( s );

That worked but aren't their better ways?

Greetings,
Rick
 
C

Christopher Benson-Manica

Rick said:
Another newbie question. I'm trying to convert a char to an integer. My
input is a string which holds a number on place 4. However, if I try
int number = atoi( input[3] );

int number=atoi( input+3 );
 
K

Karl Heinz Buchegger

Rick said:
Hi,

Another newbie question. I'm trying to convert a char to an integer. My
input is a string which holds a number on place 4. However, if I try
int number = atoi( input[3] );
I'll get an error because of the type conversion I guess. Atoi wants a
string while I put a single char into it. So I tried it another way:
char s[2];
int number;
s[0] = input[3];
s[1] = 0;
number = atoi( s );

That worked but aren't their better ways?

int number = input[3] - '0';
 
M

Mark Gordon

Another newbie question. I'm trying to convert a char to an integer.
My input is a string which holds a number on place 4. However, if I
try
int number = atoi( input[3] );
I'll get an error because of the type conversion I guess. Atoi wants a
string while I put a single char into it. So I tried it another way:
char s[2];
int number;
s[0] = input[3];
s[1] = 0;
number = atoi( s );

That worked but aren't their better ways?

That depends on what you mean by "convert a char to an integer". If you
want to convert a single character in the range '0' to '9' then
int val='5'-'0'; /* val is guaranteed to be 5 */
So for your example
/* do some data validation */
number = input[3] - '0';

NOTE: The whilst the ordering of characters '0' to '9' is guaranteed by
the standard the rest of the character set has no such guarantee so, for
example, reporting which letter of the alphabet 'c' is requires
something a bit more than 1 + 'c' - 'a'
 
D

Dan Pop

In said:
Another newbie question. I'm trying to convert a char to an integer. My
input is a string which holds a number on place 4. However, if I try
int number = atoi( input[3] );
I'll get an error because of the type conversion I guess. Atoi wants a
string while I put a single char into it. So I tried it another way:
char s[2];
int number;
s[0] = input[3];
s[1] = 0;
number = atoi( s );

That worked but aren't their better ways?

For single digit conversions, subtracting '0' (quotes included) from the
char value is guaranteed to provide the correct result, as long as the
char value really corresponds to a digit. In other words, C guarantees
that the value of each digit is one greater than the value of the previous
regardless of the character set used by the implementation.

Note that the usage of atoi is usually restricted to newbies or to those
cases where the string is known to contain a valid number: its behaviour
on erroneous input is suboptimal.

Dan
 
I

Isaac Mushinsky

Christopher said:
Rick said:
Another newbie question. I'm trying to convert a char to an integer. My
input is a string which holds a number on place 4. However, if I try
int number = atoi( input[3] );

int number=atoi( input+3 );


Sis you mean a digit at postion 3? Or a null-termnated number starting at
intput+3? This will fail in the case of a single digit unless input[4] is
null.
 

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

Similar Threads

a question abou "atoi" 6
atoi 11
integer overflow in atoi 29
Print with command-line arguments 0
Help with atoi function for a numero program. 4
question about atoi 3
Problem with atoi() 25
atoi query 13

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,185
Latest member
GluceaReviews

Latest Threads

Top