String Array to Binary Number Conversion

D

Delali Dzirasa

Does anyone know of the fastest way to convert a CString of numbers ie
"00000001" to the binary representation in an int?

for example

CString myVal = "00000001";

int myIntVal = 0;

myIntVal = CStringToBinary(myVal);

where the "CStringToBinary" function is what I am looking to find.


the int representation should be 1 or in hex 0x01


Any help is greatly appreciated.


Delali
 
M

Mike Wahler

Delali Dzirasa said:
Does anyone know of the fastest way to convert a CString of numbers ie
"00000001" to the binary representation in an int?

There's no 'CString' type in standard C++. I can show you
how to convert a 'std::string' containing text representing
a binary value to an integer type.

You'll need to figure out how to convert from 'CString'
to 'std::string'. This is indeed possible, look up
the 'CString' and 'std::string' member functions.


Also, since there's no way to determine the which binary
representation an implementation uses, it doesn't really make sense
to try to represent the binary pattern of a signed type
like that. So my example below uses an unsigned int instead
of signed.

for example

CString myVal = "00000001";

int myIntVal = 0;

myIntVal = CStringToBinary(myVal);

where the "CStringToBinary" function is what I am looking to find.


the int representation should be 1 or in hex 0x01


Any help is greatly appreciated.

#include <bitset>
#include <climits>
#include <iostream>
#include <stdexcept>
#include <string>

unsigned int binstr2uint(const std::string& s)
{
const std::string::size_type
max_bits(sizeof(unsigned int) * CHAR_BIT);

if(s.size() > max_bits)
throw "[binstr2uint]: Too many digits in input\n";

return (std::bitset<max_bits>(s).to_ulong());
}

int main()
{
std::string s("101010101");

try
{
unsigned int i(binstr2uint(s));

std::cout << "s == " << s << '\n'
<< "i == " << i << '\n';

}
catch(const char *e)
{
std::cout << e << '\n';
}
catch(std::invalid_argument&)
{
std::cout << "[binstr2uint]: Invalid digit in input\n";
}

return 0;
}


Output:

s == 101010101
i == 341


-Mike
 
C

Carl Muller

Delali Dzirasa said:
Does anyone know of the fastest way to convert a CString of numbers ie
"00000001" to the binary representation in an int?

for example

CString myVal = "00000001";

int myIntVal = 0;

myIntVal = CStringToBinary(myVal);
where the "CStringToBinary" function is what I am looking to find.

the int representation should be 1 or in hex 0x01
Any help is greatly appreciated.

Delali

int myIntVal = 0;
const char* ptr = myVal;
while (*ptr)
myIntVal += myIntVal + (*ptr++ - '0');

Of course that only works for binary numbers :)
 
D

Delali Dzirasa

this worked great!

Thanks!
Delali

Carl Muller said:
"Delali Dzirasa" <[email protected]> wrote in message

int myIntVal = 0;
const char* ptr = myVal;
while (*ptr)
myIntVal += myIntVal + (*ptr++ - '0');

Of course that only works for binary numbers :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top