Binary String in to Integer in c++

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2007
Posts: 17
Reputation: MukeshZ is an unknown quantity at this point 
Solved Threads: 0
MukeshZ MukeshZ is offline Offline
Newbie Poster

Binary String in to Integer in c++

 
0
  #1
Apr 27th, 2007
hi

I want convert Binary String to intger Format in c++.
like "00000100" should give me as "04" or "4"
could anyone suggest me the solution for it.

thanks
Mukesh
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Binary String in to Integer in c++

 
0
  #2
Apr 27th, 2007
construct a bitset using the string and then convert it to a ulong.
  1. #include <iostream>
  2. #include <string>
  3. #include <bitset>
  4. #include <limits>
  5. using namespace std ;
  6.  
  7. int main()
  8. {
  9. string str = "00000100" ;
  10. cout << bitset<numeric_limits<unsigned long>::digits>(str).to_ulong()
  11. << '\n' ;
  12. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 5742 | Replies: 1
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC