String Array to Binary Number Conversion

Discussion in 'C++' started by Delali Dzirasa, Sep 10, 2003.

  1. 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
     
    Delali Dzirasa, Sep 10, 2003
    #1
    1. Advertising

  2. Kevin Goodsell, Sep 10, 2003
    #2
    1. Advertising

  3. Delali Dzirasa

    Mike Wahler Guest

    Delali Dzirasa <> wrote in message
    news:bjnph7$j8p$...
    > 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
     
    Mike Wahler, Sep 10, 2003
    #3
  4. Delali Dzirasa

    Carl Muller Guest

    "Delali Dzirasa" <> wrote in message news:<bjnph7$j8p$>...
    > 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 :)
     
    Carl Muller, Sep 11, 2003
    #4
  5. this worked great!

    Thanks!
    Delali

    "Carl Muller" <> wrote in message
    news:...
    > "Delali Dzirasa" <> wrote in message

    news:<bjnph7$j8p$>...
    > > 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 :)
     
    Delali Dzirasa, Sep 15, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. makok
    Replies:
    1
    Views:
    6,789
    Anders Hellerup Madsen
    Feb 23, 2004
  2. Alexander Eisenhuth
    Replies:
    5
    Views:
    541
    Bob Gailer
    Jul 25, 2003
  3. Replies:
    14
    Views:
    1,044
    Simon Biber
    Nov 16, 2006
  4. fermineutron

    Large number conversion, ASCII to binary

    fermineutron, Jul 14, 2008, in forum: C Programming
    Replies:
    10
    Views:
    1,639
    Ben Bacarisse
    Jul 15, 2008
  5. Bob Sanders
    Replies:
    5
    Views:
    222
    Ryan Lewis
    May 30, 2008
Loading...

Share This Page