Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > String Array to Binary Number Conversion

Reply
Thread Tools

String Array to Binary Number Conversion

 
 
Delali Dzirasa
Guest
Posts: n/a
 
      09-10-2003
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


 
Reply With Quote
 
 
 
 
Kevin Goodsell
Guest
Posts: n/a
 
      09-10-2003
Delali Dzirasa wrote:

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


Here we discuss standard C++. We don't know what 'CString' is.

http://www.slack.net/~shiva/welcome.txt
http://www.parashift.com/c++-faq-lite/
http://www.slack.net/~shiva/offtopic.txt

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      09-10-2003

Delali Dzirasa <(E-Mail Removed)> wrote in message
news:bjnph7$j8p$(E-Mail Removed)...
> 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



 
Reply With Quote
 
Carl Muller
Guest
Posts: n/a
 
      09-10-2003
"Delali Dzirasa" <(E-Mail Removed)> wrote in message news:<bjnph7$j8p$(E-Mail Removed)>...
> 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
 
Reply With Quote
 
Delali Dzirasa
Guest
Posts: n/a
 
      09-15-2003
this worked great!

Thanks!
Delali

"Carl Muller" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...
> "Delali Dzirasa" <(E-Mail Removed)> wrote in message

news:<bjnph7$j8p$(E-Mail Removed)>...
> > 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
conversion of real number into binary number rajeswari01 Hardware 1 07-15-2011 05:30 AM
conversion of real number into binary number rajeswari01 Hardware 0 10-15-2010 12:30 AM
Large number conversion, ASCII to binary fermineutron C Programming 10 07-15-2008 02:12 AM
Convert decimal number in binary number makok VHDL 1 02-23-2004 06:04 PM
data conversion question (binary string to 'real string') Alexander Eisenhuth Python 5 07-25-2003 06:42 PM



Advertisments