954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Binary String in to Integer in c++

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:)

MukeshZ
Newbie Poster
17 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

construct a bitset using the string and then convert it to a ulong.

#include <iostream>
#include <string>
#include <bitset>
#include <limits>
using namespace std ;

int main()
{
  string str = "00000100" ;
  cout << bitset<numeric_limits<unsigned long>::digits>(str).to_ulong() 
         << '\n' ;
}
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You