473,493 Members | 4,347 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

converting a character in a string to an integer

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
4 1442
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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
11606
by: p s | last post by:
hi all i have a vb6 project, one of the functions is to read in a text file and place it into an array problem is, when people use the TAB key in the original file that is read, i just get the...
12
70511
by: David Williams | last post by:
Hi all, i have been able to convert an ASCII character to an INT however im lost as to how to change them back. Cant find anything on the net (though im probably looking in the wrong places!)....
4
3291
by: Cyde Weys | last post by:
I'm currently working on converting a simulator program from Visual Basic 6.0 to Visual C++ .NET. I've figured out most of the stuff, but there's still one thing I haven't gotten to and I've never...
7
8668
by: RCS | last post by:
Okay, a rather 'interesting' situation has arisen at a place I work: I need to convert a database from Access to something that can be used over the web. I am currently maintaining and...
2
5737
by: Asbjørn Ulsberg | last post by:
Hi. I'm trying to convert Brady Hegberg's great RTF2HTML VB 6.0 module to C#. I've managed to convert the VB code to VB.NET, which gave me the following code: Option Strict On Option...
5
4153
by: David | last post by:
I note that you can null teminate a string by adding controlchar.null. Is there a way of adding a null to a Buffer of Bytes and converting it to a string. I have packets coming in from a...
11
3337
by: TomServo | last post by:
I am writing code that needs to run on a variety of Unix systems. I am calling the statvfs and statfs system calls and I need to to convert some of the integers returned to character strings....
12
2572
by: Rob Meade | last post by:
Hi all, Ok - I've come from a 1.1 background - and previously I've never had any problem with doing this: Response.Write (Session("MyDate").ToString("dd/MM/yyyy")) So, I might get this for...
2
3985
by: CoreyWhite | last post by:
Problem: You have numbers in string format, but you need to convert them to a numeric type, such as an int or float. Solution: You can do this with the standard library functions. The...
5
32869
by: Hemant Shah | last post by:
Folks, How can I convert date/time/timestamp to an integer? According to UDB 8.1 docs that I have, INTEGER('1964-07-20') should return 19640720, but when I run the SQL statement I get...
0
7118
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6980
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7192
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7364
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5452
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4886
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
282
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.