473,670 Members | 2,228 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 2313
Rick <as******@hotma il.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)cybers pace.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************ ***********@new s.xs4all.nl> "Rick" <as******@hotma il.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******@hotma il.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******@hotma il.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
7436
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()" for more information. > > **************************************************************** > Personal firewall software may warn about the connection IDLE > makes to its subprocess using this computer's internal loopback
4
1604
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 "%u" is not correct. anybody know a thing or two about this. regards
6
3449
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 it. I have marked the error line. Thank you very much for your help. void evaluates(char *postfix) { int position; char number1, number2, number3=0;
47
46180
by: sudharsan | last post by:
Hi could you please explain wat atoi( ) function is for and an example how to use it?
4
2456
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
4183
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 to integer Parses the C string str interpreting its content as an integral
4
2511
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 will be a digit for which there are known characteristics which i will print.
50
5215
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 success or not. Am I wrong? Bill
10
4170
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 string,not other string. I've written these sentences: int a;
0
8469
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8903
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8592
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7419
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6213
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4211
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2800
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2042
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.