Converting string of 1s and 0s into binary value

前端 未结 6 907
梦如初夏
梦如初夏 2020-11-27 07:06

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 s

相关标签:
6条回答
  • 2020-11-27 07:46

    I've wrote a few functions for this and licensed under LGPL. Can be found here.

    0 讨论(0)
  • 2020-11-27 07:49
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
        char * ptr;
        long parsed = strtol("11110111", & ptr, 2);
        printf("%lX\n", parsed);
        return EXIT_SUCCESS;
    }
    

    For larger numbers, there as a long long version, strtoll.

    0 讨论(0)
  • 2020-11-27 07:51

    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;
    }
    
    0 讨论(0)
  • 2020-11-27 07:59
    #include <iostream>
    #include <stdio.h>
    #include <string>
    
    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;
    }
    
    0 讨论(0)
  • 2020-11-27 08:01

    You can use Boost Dynamic Bitset:

    boost::dynamic_bitset<>  x(std::string("01011"));
    std::cout << x << ":" << x.to_ulong() << std::endl;
    
    0 讨论(0)
  • 2020-11-27 08:06

    You can use strtol

    char string[] = "1101110100110100100000";
    char * end;
    long int value = strtol (string,&end,2);
    
    0 讨论(0)
提交回复
热议问题