6

I want to convert 64 bit binary string to 64 bit integer (unsigned). Is there any library function to do that in C++ ?

Edit:

I use:

main()
{
std::string st = "010111000010010011000100010001110110011010110001010111010010010110100101011001010110010101101010" ;

uint64_t number;
number = strtoull (st.c_str (),NULL,2);
cout << number << " " ;

char ch = std::cin.get();
cout << ch ;


   return 0;
}
alessandro
  • 1,681
  • 10
  • 33
  • 54
  • What do you mean by a "binary string"? A string containing the characters '0' and '1'? – Jerry Coffin Nov 26 '12 at 14:35
  • 5
    You have 96 bits. That's larger than any integer type most C++ implementations supports. – nos Nov 26 '12 at 15:08
  • possible duplicate of [How to convert a number to string and vice versa in C++](http://stackoverflow.com/questions/5290089/how-to-convert-a-number-to-string-and-vice-versa-in-c) – Kate Gregory Nov 26 '12 at 16:21

5 Answers5

12

You can use strtoull() function with base 2 (there is an example if you follow the link).

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
  • Assuming that the size of an `unsigned long long` is 64 bits, which is likely. – Gorpik Nov 26 '12 at 14:33
  • @Gorpik That's the largest integral type C/C++ standard library can parse numbers into anyway. – Maxim Egorushkin Nov 26 '12 at 14:48
  • @alessandro Please post the code so that we can see what and how you do. – Maxim Egorushkin Nov 26 '12 at 14:52
  • @alessandro You'll have to be a bit more specific than "wrong output". Maxim: Indeed. Just wanted to make it clear that the answer is not universally valid, it depends on the platform. – Gorpik Nov 26 '12 at 14:53
  • I have given the code in my OP. Wrong output means output value is not that one what it should be (found using some binary converter). – alessandro Nov 26 '12 at 14:58
10

If you have C++11 - you can use std::bitset for example.

#include <iostream>
#include <bitset>

int main()
{
    const std::string s = "0010111100011100011";
    unsigned long long value = std::bitset<64>(s).to_ullong();
    std::cout << value << std::endl;
}

or std::stoull

#include <iostream>
#include <string>

int main()
{
    const std::string s = "0010111100011100011";
    unsigned long long value = std::stoull(s, 0, 2);
    std::cout << value << std::endl;
}
ForEveR
  • 55,233
  • 2
  • 119
  • 133
1

The following code is probably the simplest way to convert binary string to its integer value. Without using biteset or boost this works for any length of binary string.

std::string binaryString = "10101010";  
int value = 0;
int indexCounter = 0;
for(int i=binaryString.length()-1;i>=0;i--){

    if(binaryString[i]=='1'){
        value += pow(2, indexCounter);
    }
    indexCounter++;
}
0

Converts binary string to integer and binary string (up to 64 bits) to long. Uses bit-shifting so slightly more efficient than pow().

#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;
}
PentiumPro200
  • 641
  • 7
  • 23
-1

You may try this. If you don't need it to be generic, you can specify the type and do away with Number:

template <typename Number>
Number process_data( const std::string& binary )
{
    const Number * data_ptr;
    Number data;

    data_ptr = reinterpret_cast<const Number*>(binary.data());
    data = *data_ptr;

    return data;
}
  • The above assumes that `std::string` is used to hold binary data, which less than ideal. People normally use `std::vector` for binary data and `std::string` for textual data. – Maxim Egorushkin Nov 26 '12 at 14:56
  • It may work, but it wouldn't pass a code review. Passing binary data around as `std::string` is at best confusing. You don't pass `int`s around as `void`s, do you? – Maxim Egorushkin Nov 26 '12 at 16:54