JASON
JASON

Reputation: 7491

What is the best way of interpreting "Bit String"

I was wondering what is the best way to interpret "bit string"?

for example:

a bit string like "1010010" is feed to the following function

void foo (string s1) {
  // some code to do bit manipulation of the bit string
}

What is the best way to do it? Many thanks!!!

Upvotes: 1

Views: 126

Answers (2)

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361812

If you just want to convert string into its integral value, then std::stoi family could help:

int value = std::stoi("10100"); //value will be 10100, not 20

If you want to manipulate bit patterns represented by string, then std::bitset might help you in some way:

std::bitset<32>  bitpattern("10100"); 

//you can manupulates bitpattern as you wish
//see the member functions of std::bitset

//you can also convert into unsigned long
unsigned long ul = bitpattern.to_ulong();  //ul will be 20, not 10100

Upvotes: 2

paulsm4
paulsm4

Reputation: 121881

I would use strtol

EXAMPLE:

  char *endptr;
  long i = strtol (mystring, &endptr, 2);

Upvotes: 0

Related Questions