String Array to Binary Number Conversion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Delali Dzirasa

    String Array to Binary Number Conversion

    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 "CStringToBinar y" function is what I am looking to find.


    the int representation should be 1 or in hex 0x01


    Any help is greatly appreciated.


    Delali


  • Kevin Goodsell

    #2
    Re: String Array to Binary Number Conversion

    Delali Dzirasa wrote:
    [color=blue]
    > Does anyone know of the fastest way to convert a CString of numbers ie
    > "00000001" to the binary representation in an int?[/color]

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





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

    Comment

    • Mike Wahler

      #3
      Re: String Array to Binary Number Conversion


      Delali Dzirasa <Delali.Dzirasa @jhuapl.edu> wrote in message
      news:bjnph7$j8p $1@houston.jhua pl.edu...[color=blue]
      > Does anyone know of the fastest way to convert a CString of numbers ie
      > "00000001" to the binary representation in an int?[/color]

      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.

      [color=blue]
      >
      > for example
      >
      > CString myVal = "00000001";
      >
      > int myIntVal = 0;
      >
      > myIntVal = CStringToBinary (myVal);
      >
      > where the "CStringToBinar y" function is what I am looking to find.
      >
      >
      > the int representation should be 1 or in hex 0x01
      >
      >
      > Any help is greatly appreciated.[/color]

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

      unsigned int binstr2uint(con st std::string& s)
      {
      const std::string::si ze_type
      max_bits(sizeof (unsigned int) * CHAR_BIT);

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

      return (std::bitset<ma x_bits>(s).to_u long());
      }

      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::inva lid_argument&)
      {
      std::cout << "[binstr2uint]: Invalid digit in input\n";
      }

      return 0;
      }


      Output:

      s == 101010101
      i == 341


      -Mike



      Comment

      • Carl Muller

        #4
        Re: String Array to Binary Number Conversion

        "Delali Dzirasa" <Delali.Dzirasa @jhuapl.edu> wrote in message news:<bjnph7$j8 p$1@houston.jhu apl.edu>...[color=blue]
        > 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 "CStringToBinar y" function is what I am looking to find.
        >
        > the int representation should be 1 or in hex 0x01
        > Any help is greatly appreciated.
        >
        > Delali[/color]

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

        Of course that only works for binary numbers :-)

        Comment

        • Delali Dzirasa

          #5
          Re: String Array to Binary Number Conversion

          this worked great!

          Thanks!
          Delali

          "Carl Muller" <carlmuller@hot mail.com> wrote in message
          news:2dd08d44.0 309101525.de842 fd@posting.goog le.com...[color=blue]
          > "Delali Dzirasa" <Delali.Dzirasa @jhuapl.edu> wrote in message[/color]
          news:<bjnph7$j8 p$1@houston.jhu apl.edu>...[color=blue][color=green]
          > > 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 "CStringToBinar y" function is what I am looking to find.
          > >
          > > the int representation should be 1 or in hex 0x01
          > > Any help is greatly appreciated.
          > >
          > > Delali[/color]
          >
          > int myIntVal = 0;
          > const char* ptr = myVal;
          > while (*ptr)
          > myIntVal += myIntVal + (*ptr++ - '0');
          >
          > Of course that only works for binary numbers :-)[/color]


          Comment

          Working...