By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,434 Members | 1,878 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,434 IT Pros & Developers. It's quick & easy.

converting a character in a string to an integer

P: n/a
Hi,

I have a string
strcpy(str, "2A");

and I want to grab the char 2 and convert it to an integer.

I tried doing this

int num = atoi(str[0]);

and it will not compile.

Any suggestions

Thanks
-R

Nov 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
In article <11**********************@f14g2000cwb.googlegroups .com>,
<ro*********@gmail.com> wrote:
I have a string
strcpy(str, "2A"); and I want to grab the char 2 and convert it to an integer. I tried doing this int num = atoi(str[0]); and it will not compile.


atoi() must be passed a const char *str, but str[0] is just a plain char.
Even with automatic promotion into a const char, that's still a difference
of char vs pointer-to-char .

What is your general rule for such inputs? That there is -exactly-
one digit, which will be followed by something that is not a digit?
That you only want to convert one digit no matter what follows
(including possibly another digit)? That there might be several
digits, followed by something that is not a digit?

atoi() stops parsing when it finds something that isn't a valid
digit... but watch out for the boundary case where the input
does not start start with a digit. See also strtoul() and kin,
which have much better error reporting.

If you have exactly one digit, you could just use
int num = str[0]-'0';

--
All is vanity. -- Ecclesiastes
Nov 23 '05 #2

P: n/a
Walter Roberson wrote:

In article <11**********************@f14g2000cwb.googlegroups .com>,
<ro*********@gmail.com> wrote:
I have a string
strcpy(str, "2A");
and I want to grab the char 2 and convert it to an integer.

I tried doing this

int num = atoi(str[0]);

and it will not compile.

What is your general rule for such inputs? That there is -exactly-
one digit, which will be followed by something that is not a digit?
That you only want to convert one digit no matter what follows
(including possibly another digit)?


If that's the rule, then it's simple.

int num = "2A"[0] - '0';

--
pete
Nov 23 '05 #3

P: n/a
On 21 Nov 2005 19:29:34 -0800, ro*********@gmail.com wrote:
Hi,

I have a string
strcpy(str, "2A");

and I want to grab the char 2 and convert it to an integer.

I tried doing this

int num = atoi(str[0]);
What type of argument does atoi take? What is the type of str[0]? If
the value in str had been "29", would you still want only the 2?

and it will not compile.

<<Remove the del for email>>
Nov 23 '05 #4

P: n/a

"pete" <pf*****@mindspring.com> wrote in message
news:43***********@mindspring.com...
Walter Roberson wrote:

In article <11**********************@f14g2000cwb.googlegroups .com>,
<ro*********@gmail.com> wrote:
>I have a string
>strcpy(str, "2A");

>and I want to grab the char 2 and convert it to an integer.

>I tried doing this

>int num = atoi(str[0]);

>and it will not compile.

What is your general rule for such inputs? That there is -exactly-
one digit, which will be followed by something that is not a digit?
That you only want to convert one digit no matter what follows
(including possibly another digit)?


If that's the rule, then it's simple.

int num = "2A"[0] - '0';


Nice.
Nov 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.