Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm trying to convert an incoming sting of 1s and 0s from stdin into their respective binary values (where a string such as "11110111" would be converted to 0xF7). This seems pretty trivial but I don't want to reinvent the wheel so I'm wondering if there's anything in the C/C++ standard libs that can already perform such an operation?

Not homework. I'm trying to embed a converter for easier read into a set of debugging tools but all our input is coming through in string format. grosauro Sep 22, 2008 at 22:13 This looks about right. My only concern was on very large strings, which thinking about it now, would probably need some custom code anyhow. grosauro Sep 22, 2008 at 22:10 Ah, now I see your point. The string length shouldn't be a problem as long as the resulting number fits into the long or long long integer. jkramer Sep 22, 2008 at 22:22 It is only a problem if your binary string is longer than 64 1 s and 0 s. Then, you'd be out of range on long long . Charles Dec 24, 2015 at 22:29

You can use std::bitset (if then length of your bits is known at compile time)
Though with some program you could break it up into chunks and combine.

#include <bitset>
#include <iostream>
int main()
    std::bitset<5>  x(std::string("01011"));
    std::cout << x << ":" << x.to_ulong() << std::endl;
using namespace std;
string getBinaryString(int value, unsigned int length, bool reverse) {
    string output = string(length, '0');
    if (!reverse) {
        for (unsigned int i = 0; i < length; i++) {
            if ((value & (1 << i)) != 0) {
                output[i] = '1';
    else {
        for (unsigned int i = 0; i < length; i++) {
            if ((value & (1 << (length - i - 1))) != 0) {
                output[i] = '1';
    return output;
unsigned long getInteger(const string& input, size_t lsbindex, size_t msbindex) {
    unsigned long val = 0;
    unsigned int offset = 0;
    if (lsbindex > msbindex) {
        size_t length = lsbindex - msbindex;
        for (size_t i = msbindex; i <= lsbindex; i++, offset++) {
            if (input[i] == '1') {
                val |= (1 << (length - offset));
    else { //lsbindex < msbindex
        for (size_t i = lsbindex; i <= msbindex; i++, offset++) {
            if (input[i] == '1') {
                val |= (1 << offset);
    return val;
int main() {
    int value = 23;
    cout << value << ": " << getBinaryString(value, 5, false) << endl;
    string str = "01011";
    cout << str << ": " << getInteger(str, 1, 3) << endl;
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.