473,513 Members | 2,561 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

atoi question

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
Nov 13 '05 #1
7 2300
Rick <as******@hotmail.com> spoke thus:
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 );

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 13 '05 #2
Yes, that's what I call a better way! Thanks!

Rick
Nov 13 '05 #3


Rick wrote:

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';
--
Karl Heinz Buchegger
kb******@gascad.at
Nov 13 '05 #4
In <3f***********************@news.xs4all.nl> "Rick" <as******@hotmail.com> writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #5
On Mon, 27 Oct 2003 13:54:06 +0100
"Rick" <as******@hotmail.com> wrote:
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'
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.
Nov 13 '05 #6
Everyone thanks again!

Rick
Nov 13 '05 #7
Christopher Benson-Manica wrote:
Rick <as******@hotmail.com> spoke thus:
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.
Nov 13 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
7423
by: Mike Moum | last post by:
I think there may be a bug in string.atoi and string.atol. Here's some output from idle. > Python 2.3.4 (#2, Jan 5 2005, 08:24:51) > on linux2 > Type "copyright", "credits" or "license()"...
4
1589
by: senthil kumar | last post by:
Hi We've been noticing a problem with atoi conversion on PowerPC. When we supply value > 2^31-1 to utmost 2^32-1 it doesn't work properly. What i mean is the converted value when printed using...
6
3441
by: Henry Jordon | last post by:
This is a piece of code that I have left to complete my project. I have hopefully one small error that needs to be fixed. This portion of the code evaluates the postfix notation that is passed to...
47
46131
by: sudharsan | last post by:
Hi could you please explain wat atoi( ) function is for and an example how to use it?
4
2450
by: sam | last post by:
Hi, whats the meaning of atoi function here. int atoi(char __ch) { switch(__ch) { case '0':return 0; case '1':return 1; case '2':return 2;
3
4169
by: pauldepstein | last post by:
The following description of atoi is pasted from cplusplus.com. My question is after the pasting. ***** PASTING BEGINS HERE ****** int atoi ( const char * str ); <cstdlib> Convert string...
4
2504
by: Ram | last post by:
Hi All, Firstly i am a newbie and trying to learn C. The background of the problem is Program: Presently I am working on a program of numerology and the I/P will be the name and output...
50
5171
by: Bill Cunningham | last post by:
I have just read atoi() returns no errors. It returns an int though and the value of the int is supposed to be the value of the conversion. It seems to me that right there tells you if there was...
10
4156
by: 66650755 | last post by:
First,thanks for all who have answered my last question. if char string="12345"; how could I convert the string(that is "3") to an int by using atoi? I only want to convert...
0
7259
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
7535
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...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7523
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...
1
5085
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
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
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.