944,135 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 18943
  • C++ RSS
Jul 19th, 2007
0

Conversion from Char* to int ?

Expand Post »
Hi All,

I want to convert char * to int.

Plz help me out.

Thanks in advance.
Similar Threads
Reputation Points: 25
Solved Threads: 0
Light Poster
RohitSahni is offline Offline
35 posts
since Jul 2007
Jul 19th, 2007
0

Re: Conversion from Char* to int ?

What exactly do you mean?
Do you
a) want to convert an array of ASCII characters into an int:

C++ Syntax (Toggle Plain Text)
  1. char *numstr = "1234";
  2. int val = atoi(numstr); // val now = 1234

b) convert the pointer to an int:
C++ Syntax (Toggle Plain Text)
  1. char *xyz; // given contents somewhere
  2. int addr = (int)xyz; // addr now = the char pointer
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007
Jul 19th, 2007
0

Re: Conversion from Char* to int ?

Thanks a lot for your Rply..i will be using atoi().
And it worked for me.
Reputation Points: 25
Solved Threads: 0
Light Poster
RohitSahni is offline Offline
35 posts
since Jul 2007
Jul 19th, 2007
1

Re: Conversion from Char* to int ?

Don't use atoi() - here's how to do it in C++
CPP Syntax (Toggle Plain Text)
  1. #include <sstream>
  2. #include <iostream>
  3.  
  4. int main()
  5. {
  6. const char* foo("1234");
  7. std::stringstream ss(foo);
  8. int i;
  9. if( ss >> i )
  10. std::cout << "i is: " << i ;
  11. else
  12. std::cout << "error";
  13. }
The reason for using stringstreams is that your conversion will fail if the string contains anything which can't be converted to an int - which, if you're dealing with user input, is a real problem. if you use a stringstream, you can detect the error, without it messing up the rest of the program.
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
Jul 19th, 2007
0

Re: Conversion from Char* to int ?

Quote ...
Don't use atoi()
Why?
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007
Jul 19th, 2007
0

Re: Conversion from Char* to int ?

I suppose I should add that atoi() has no reliable way of handling any errors that might be thrown up when your conversion fails. This is where stringstreams provide a far more robust solution, that you can test for failure before using the retrieved value.
Last edited by Bench; Jul 19th, 2007 at 9:59 am.
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
Jul 19th, 2007
1

Re: Conversion from Char* to int ?

> Why?
Answer.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Can not enter multiple data into nested while loop
Next Thread in C++ Forum Timeline: Quick question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC