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;
}
3
  • What do you mean by a "binary string"? A string containing the characters '0' and '1'? Commented Nov 26, 2012 at 14:35
  • 5
    You have 96 bits. That's larger than any integer type most C++ implementations supports. Commented Nov 26, 2012 at 15:08
  • possible duplicate of How to convert a number to string and vice versa in C++ Commented Nov 26, 2012 at 16:21

5 Answers 5

12

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

Sign up to request clarification or add additional context in comments.

5 Comments

Assuming that the size of an unsigned long long is 64 bits, which is likely.
@Gorpik That's the largest integral type C/C++ standard library can parse numbers into anyway.
@alessandro Please post the code so that we can see what and how you do.
@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.
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).
11

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;
}

Comments

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++;
}

Comments

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;
}

Comments

-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;
}

2 Comments

The above assumes that std::string is used to hold binary data, which less than ideal. People normally use std::vector<char> for binary data and std::string for textual data.
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 ints around as voids, do you?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.