Conversion from Char* to int ?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2007
Posts: 35
Reputation: RohitSahni is an unknown quantity at this point 
Solved Threads: 0
RohitSahni RohitSahni is offline Offline
Light Poster

Conversion from Char* to int ?

 
0
  #1
Jul 19th, 2007
Hi All,

I want to convert char * to int.

Plz help me out.

Thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Conversion from Char* to int ?

 
0
  #2
Jul 19th, 2007
What exactly do you mean?
Do you
a) want to convert an array of ASCII characters into an int:

  1. char *numstr = "1234";
  2. int val = atoi(numstr); // val now = 1234

b) convert the pointer to an int:
  1. char *xyz; // given contents somewhere
  2. int addr = (int)xyz; // addr now = the char pointer
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 35
Reputation: RohitSahni is an unknown quantity at this point 
Solved Threads: 0
RohitSahni RohitSahni is offline Offline
Light Poster

Re: Conversion from Char* to int ?

 
0
  #3
Jul 19th, 2007
Thanks a lot for your Rply..i will be using atoi().
And it worked for me.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 490
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 49
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Conversion from Char* to int ?

 
1
  #4
Jul 19th, 2007
Don't use atoi() - here's how to do it in C++
  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.
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: Conversion from Char* to int ?

 
0
  #5
Jul 19th, 2007
Don't use atoi()
Why?
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 490
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 49
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Conversion from Char* to int ?

 
0
  #6
Jul 19th, 2007
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.
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,629
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Conversion from Char* to int ?

 
1
  #7
Jul 19th, 2007
> Why?
Answer.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC