C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
char to int Posted by adeydas on 3 Feb 2006 at 1:54 PM
Hi!
is there a way to convert value of a string into an integer? i am doing a simultaneous equation program that needs this.

Thank you.
Abhishek
http://deydas.net
Report
Re: char to int Posted by MT2002 on 3 Feb 2006 at 5:36 PM
: Hi!
: is there a way to convert value of a string into an integer? i am doing a simultaneous equation program that needs this.
:
: Thank you.
: Abhishek
: http://deydas.net
:

Try strtol declared in <stdlib.h>. strtol
has the form..

long strtol(const char * s, char * *endptr, int radix);

where s is the string, endptr is a ptr
to buffer to store first non-int char, radix is
the base of the number to convert it to.

For example...
#include <cstdlib>

// convert string "123" into int
int iValue = strtol ("123\0",0 ,10);

Hope this helps;
~mt2002

Report
Re: char to int Posted by stober on 3 Feb 2006 at 5:41 PM
atoi() is even easier, providing the string is all decimal digits -- no hex or octal.
char str[] = "1234";
int n = atoi(str);

Report
Re: char to int Posted by MT2002 on 3 Feb 2006 at 6:44 PM
: atoi() is even easier, providing the string is all decimal digits -- no hex or octal.
:
: char str[] = "1234";
: int n = atoi(str);
: 

:

I just want to note that atoi() isnt ANSI compatible,
however most compilers support it. I heard some GCC
compilers dont, though (I dont use GCC).

Report
Re: char to int Posted by stober on 3 Feb 2006 at 8:19 PM
:
: I just want to note that atoi() isnt ANSI compatible,
: however most compilers support it. I heard some GCC
: compilers dont, though (I dont use GCC).

:


yes it is ansi C --

http://www.cplusplus.com/ref/cstdlib/atoi.html

Report
Re: char to int Posted by MT2002 on 3 Feb 2006 at 8:24 PM
: :
: : I just want to note that atoi() isnt ANSI compatible,
: : however most compilers support it. I heard some GCC
: : compilers dont, though (I dont use GCC).

: :
:
:
: yes it is ansi C --
:
: http://www.cplusplus.com/ref/cstdlib/atoi.html

Your right--I misread atoi() as itoa()!

Report
Re: char to int Posted by pseudocoder on 5 Feb 2006 at 9:19 PM
: Hi!
: is there a way to convert value of a string into an integer? i am doing a simultaneous equation program that needs this.
:
: Thank you.
: Abhishek
: http://deydas.net
:

If you're using 0 to fill in missing terms, then sscanf will return the number of conversions instead of a value.

#include <stdio.h> 
#include <string.h>

int main(void)
{
   
   int n;
   
   printf("hex: %2d\n", sscanf("FF", "%x", &n) == 1 ? n : -1); 
   printf("oct: %2d\n", sscanf("377", "%o", &n) == 1 ? n : -1);
   printf("dec: %2d\n", sscanf("255", "%d", &n) == 1 ? n : -1); 
   
   return (0);
}  


It *should* work with dec, hex, or oct values.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.