Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Closed Thread
 
Thread Tools Display Modes
Old Jan 30th, 2006, 11:51 PM   #1
King
Professional Programmer
 
King's Avatar
 
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 376
Rep Power: 0 King is an unknown quantity at this point
Question Convert char[] to int

How can I convert a char[] to int without using atoi?
__________________
I am Addicted to Linux!
King is offline  
Old Jan 30th, 2006, 11:54 PM   #2
Jerkstore
Newbie
 
Join Date: Dec 2005
Posts: 12
Rep Power: 0 Jerkstore is on a distinguished road
int(variable)
Jerkstore is offline  
Old Jan 30th, 2006, 11:59 PM   #3
King
Professional Programmer
 
King's Avatar
 
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 376
Rep Power: 0 King is an unknown quantity at this point
doesnt work
__________________
I am Addicted to Linux!
King is offline  
Old Jan 31st, 2006, 12:06 AM   #4
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 852
Rep Power: 4 The Dark is on a distinguished road
Why would you want to? Perhaps for a programming class?

Think about how you might approach this if you were doing it step by step on paper. One way is to go through each character in the char[] and convert it to an integer value, keeping a running total in an int. Each time you process a new character, multiply the current running total by 10 and add in the integer value of the char.

Give it a go and if you get into trouble, post your code here.
The Dark is offline  
Old Jan 31st, 2006, 3:20 AM   #5
ivan
Professional Programmer
 
ivan's Avatar
 
Join Date: Sep 2005
Location: serbia & montenegro
Posts: 484
Rep Power: 4 ivan is on a distinguished road
Do you want to create your function or only use a different one from atoi()?
ivan is offline  
Old Jan 31st, 2006, 4:28 AM   #6
sunnypalsingh
Programmer
 
sunnypalsingh's Avatar
 
Join Date: Dec 2005
Posts: 33
Rep Power: 0 sunnypalsingh is on a distinguished road
Use strtol function. Its safer than atoi.
__________________
The only real valuable thing is intuition.
-Albert Einstein
sunnypalsingh is offline  
Old Jan 31st, 2006, 7:31 AM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
Q. How can I convert a char[] to int without using atoi?
A. int(variable)
This is the kind of crap answer I'm talking about. Please don't do this. You do neither yourself nor the questioner any good.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline  
Old Jan 31st, 2006, 8:16 AM   #8
King
Professional Programmer
 
King's Avatar
 
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 376
Rep Power: 0 King is an unknown quantity at this point
I got it working, thanks for the replies
__________________
I am Addicted to Linux!
King is offline  
Old Jan 31st, 2006, 8:42 AM   #9
clsk
Newbie
 
Join Date: Jan 2006
Posts: 13
Rep Power: 0 clsk is on a distinguished road
If you're using doing this in c++ why are you even using a char array to manage strings of characters?
I recommend you use std::string to manage strings of characters.
To convert integer values that are represented in a string use std::stringstream.

Here's a function I normally use to do this.
You might want to make it inline.
int StringToInt(const std::string& str) // string to int
{
  stringstream l_ss(str);
  int l_rint;
  l_ss >> l_rint; // casting from string to int...

return l_rint;
}
To do the opposite you'd use a output stringstream or std:: ostringstream.
clsk is offline  
Old Jan 31st, 2006, 9:38 AM   #10
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
Quote:
Originally Posted by Jerkstore
int(variable)
I recommend checking what language a question is for, before giving stupid and wrong answers.
Polyphemus_ is offline  
Closed Thread

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:51 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC