LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 04-07-2005, 05:11 PM   #1
fatman
Member
 
Registered: Mar 2003
Location: PA
Distribution: Ubuntu (x2)
Posts: 158

Rep: Reputation: 30
C++ string to integer conversion


Is there a function, accessible in a common library, to convert from a string (of numbers) into an integer?
I mean an string library string, not an array of char's (so atoi is not an option)
I can do it manually, but that is kinda hacky as im sure i dont do proper error checking
I am just trying to find somethig universal and simple so i can go:

#include<magiclibrary>
int i;
string s = "123";
i = magicFunction(s);

Does such a creature exist in a librarry common to all compliers?
 
Old 04-07-2005, 05:27 PM   #2
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
the magicfunction is atoi()

prototype:

int atoi(string str)

i = atoi(s);

its in stdlib.h,but i think c++ compilers will want you to use cstdlib

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

here is a complete list of the function in stdlib.h
 
Old 04-07-2005, 09:32 PM   #3
fatman
Member
 
Registered: Mar 2003
Location: PA
Distribution: Ubuntu (x2)
Posts: 158

Original Poster
Rep: Reputation: 30
the problem with atoi is that it takes a "const char*" , where the string type is a "char*"

I think this may work:

string str = "123";
int i;
i = atoi(str.c_str());
 
Old 04-08-2005, 12:49 AM   #4
FishBoy
Member
 
Registered: Nov 2004
Distribution: SuSE 10.2
Posts: 56

Rep: Reputation: 15
well you could write your own code to do that too i guess
maybe something like

Code:
//atoi.c
#include<stdio.h>

int main(){
        char st[]="123";
        int i=0;

        for(;i<(int)strlen(st)+1;i++)
                printf("%d ",st[i]); //you could replace %d with whatever you want
        printf("\n");
        return 0;
}
that code should do it it'll convert any character into a number giving you it's ascii value

Last edited by FishBoy; 04-08-2005 at 02:19 AM.
 
Old 04-08-2005, 07:44 AM   #5
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
fatman is right, i forgot about that.
 
Old 04-08-2005, 08:17 AM   #6
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282

Rep: Reputation: 61
In C++ you could use string stream input capabilities :
Code:
#include <iostream>
#include <sstream>
using namespace std;

int main() {
    string s = "123";
    int i;
    istringstream input;

    input.str(s);
    input >> i;
    cout << "i = " << i << endl;

    return 0;
}
 
Old 06-18-2006, 08:17 AM   #7
Christmas
LQ Newbie
 
Registered: Apr 2006
Location: Romania, Bucharest
Distribution: Kubuntu 6.06 Dapper Drake
Posts: 7

Rep: Reputation: 0
What about this one I made? I'm still kind of newbie and learning so I hope it's not too long or buggy.
Code:
/*
	This function converts a string to an int.
	If the string contains not only numbers, but other
	signs (including "+" or "-"), the function will
	return 0.
*/

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

int string_to_int (char s[4])
{
	int n, i;
	n = 0;
	i = 0;
	while (i < strlen (s))
	{
		if (s[i] == 48)
			n = n * 10 + 0;
		if (s[i] == 49)
			n = n * 10 + 1;
		if (s[i] == 50)
			n = n * 10 + 2;
		if (s[i] == 51)
			n = n * 10 + 3;
		if (s[i] == 52)
			n = n * 10 + 4;
		if (s[i] == 53)
			n = n * 10 + 5;
		if (s[i] == 54)
			n = n * 10 + 6;
		if (s[i] == 55)
			n = n * 10 + 7;
		if (s[i] == 56)
			n = n * 10 + 8;
		if (s[i] == 57)
			n = n * 10 + 9;
		i = i + 1;
	}
	return n;
}

main ()
{
	char s[4];
	strcpy (s, "1453");
	printf ("The string is %s\n", s);
	printf ("The number is %i\n", string_to_int (s));
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C how to convert an integer to string? totti10 Programming 10 11-10-2005 05:49 AM
convert string to integer with python Kanaflloric Programming 2 05-27-2005 11:04 AM
Integer To a String in C Bean101 Programming 2 05-27-2004 04:46 AM
How do you convert a string to an integer? wbdune Linux - Newbie 11 11-01-2003 02:01 PM
convert integer to string (atoi?) lea Programming 6 10-11-2002 05:16 PM


All times are GMT -5. The time now is 03:10 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration