String --> Byte array

Discussion in 'C++' started by qazmlp, Aug 26, 2003.

  1. qazmlp

    qazmlp Guest

    How can string be converted into a byte array in C++ ?
    Can I used std::bitset<> to do it ?
     
    qazmlp, Aug 26, 2003
    #1
    1. Advertising

  2. qazmlp

    Ron Natalie Guest

    "qazmlp" <> wrote in message news:...
    > 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());
     
    Ron Natalie, Aug 26, 2003
    #2
    1. Advertising

  3. qazmlp

    Mike Wahler Guest

    qazmlp <> wrote in message
    news:...
    > How can string be converted into a byte array in C++ ?
    > Can I used std::bitset<> to do it ?


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

    -Mike
     
    Mike Wahler, Aug 26, 2003
    #3
  4. qazmlp wrote:
    > How can string be converted into a byte array in C++ ?
    > Can I used std::bitset<> to do it ?


    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::iterator iter_array = foo.begin();

    iter_array[ 2 ] = 'x';

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

    }
     
    Gianni Mariani, Aug 27, 2003
    #4
  5. "Ron Natalie" <> wrote in message
    news:3f4b6a34$0$23182$...
    |
    | "qazmlp" <> wrote in message
    news:...
    | > 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
     
    Chris \( Val \), Aug 27, 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. Bharat Bhushan

    Appending byte[] to another byte[] array

    Bharat Bhushan, Aug 5, 2003, in forum: Java
    Replies:
    15
    Views:
    40,273
    Roedy Green
    Aug 5, 2003
  2. Kirby
    Replies:
    3
    Views:
    653
    Kirby
    Oct 8, 2004
  3. Replies:
    20
    Views:
    9,812
    licebmi
    Sep 7, 2009
  4. Tom McGlynn
    Replies:
    4
    Views:
    861
    Mark Space
    Apr 19, 2008
  5. Patricia Shanahan
    Replies:
    0
    Views:
    392
    Patricia Shanahan
    Apr 17, 2008
Loading...

Share This Page