String --> Byte array

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

    String --> Byte array

    How can string be converted into a byte array in C++ ?
    Can I used std::bitset<> to do it ?
  • Ron Natalie

    #2
    Re: String --&gt; Byte array


    "qazmlp" <qazmlp1209@red iffmail.com> wrote in message news:db9bbf31.0 308260522.58e07 e63@posting.goo gle.com...[color=blue]
    > How can string be converted into a byte array in C++ ?
    > Can I used std::bitset<> to do it ?[/color]

    Yoiu can just call data() to get a transient const char* that points
    to the beginning of a character array. Alternatively, you can copy
    it to a vector:

    string s = ...

    vector<char> v(s.size());
    copy(s.begin(), s.end(), v.begin());


    Comment

    • Mike Wahler

      #3
      Re: String --&gt; Byte array


      qazmlp <qazmlp1209@red iffmail.com> wrote in message
      news:db9bbf31.0 308260522.58e07 e63@posting.goo gle.com...[color=blue]
      > How can string be converted into a byte array in C++ ?
      > Can I used std::bitset<> to do it ?[/color]

      std::string s("Hello");
      char *array = new char[s.size()];
      std::copy(s.beg in(), s.end(), array, array + s.size());
      /* etc */
      delete [] array;

      -Mike



      Comment

      • Gianni Mariani

        #4
        Re: String --&gt; Byte array

        qazmlp wrote:[color=blue]
        > How can string be converted into a byte array in C++ ?
        > Can I used std::bitset<> to do it ?[/color]

        A string IS an array of 'string::value_ type' underneath.

        Why do you need to convert it ?

        Here is an example - you can use the method
        "data()" and return a const char *

        Or, why not just use iterators as arrays ? (second example).



        #include <iostream>
        #include <string>


        int main()
        {
        std::string foo( "ABC" );

        //examp 1
        const char * array = foo.data();

        array[0] + array[2];

        //examp 2
        std::string::it erator iter_array = foo.begin();

        iter_array[ 2 ] = 'x';

        std::cout << foo << '\n';

        }


        Comment

        • Chris \( Val \)

          #5
          Re: String --&gt; Byte array


          "Ron Natalie" <ron@sensor.com > wrote in message
          news:3f4b6a34$0 $23182$9a6e19ea @news.newshosti ng.com...
          |
          | "qazmlp" <qazmlp1209@red iffmail.com> wrote in message
          news:db9bbf31.0 308260522.58e07 e63@posting.goo gle.com...
          | > How can string be converted into a byte array in C++ ?
          | > Can I used std::bitset<> to do it ?
          |
          | Yoiu can just call data() to get a transient const char* that points
          | to the beginning of a character array. Alternatively, you can copy
          | it to a vector:
          |
          | string s = ...
          |
          | vector<char> v(s.size());
          | copy(s.begin(), s.end(), v.begin());

          Alternatively:

          vector<char> v( s.begin(), s.end() );

          Cheers.
          Chris Val


          Comment

          Working...