Convert std::string to std::vector<unsigned char> and vice versa

Discussion in 'C++' started by timor.super@gmail.com, Apr 2, 2007.

  1. Guest

    Hi group,

    how to convert a string to a vector of unsigned char ?
    I used to iterate trough the string to set the vector, but I think
    this is not the best way to do this.
    I'm a beginner with the STL

    How about allocating the vector ? should I know before the size ?

    And I would like to do then opposite conversion, from an unsigned char
    vector to a string.
    How to do this ? how about special caracters ? (\n for example)

    Thanks for your help


    Best regards,

    N.
     
    , Apr 2, 2007
    #1
    1. Advertising

  2. * :
    >
    > how to convert a string to a vector of unsigned char ?


    std::string s = "abra kadabra";
    std::vector<unsigned char> v( s.begin(), s.end() );


    > I used to iterate trough the string to set the vector, but I think
    > this is not the best way to do this.
    > I'm a beginner with the STL
    >
    > How about allocating the vector ? should I know before the size ?


    Huh.


    > And I would like to do then opposite conversion, from an unsigned char
    > vector to a string.
    > How to do this ?


    std::string t( v.begin(), v.end() );


    > how about special caracters ? (\n for example)


    Irrelevant.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Apr 2, 2007
    #2
    1. Advertising

  3. On 2 Apr, 14:20, wrote:
    > Hi group,
    >
    > how to convert a string to a vector of unsigned char ?
    > I used to iterate trough the string to set the vector, but I think
    > this is not the best way to do this.
    > I'm a beginner with the STL
    >
    > How about allocating the vector ? should I know before the size ?


    If you have very large strings you can run v.reserve(s.size()) before
    inserting, which might give a speed improvement. But you don't have
    to.

    --
    Erik Wikström
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Apr 2, 2007
    #3
  4. Guest

    On 2 avr, 15:37, "Erik Wikström" <> wrote:
    > On 2 Apr, 14:20, wrote:
    >
    > > Hi group,

    >
    > > how to convert a string to a vector of unsigned char ?
    > > I used to iterate trough the string to set the vector, but I think
    > > this is not the best way to do this.
    > > I'm a beginner with the STL

    >
    > > How about allocating the vector ? should I know before the size ?

    >
    > If you have very large strings you can run v.reserve(s.size()) before
    > inserting, which might give a speed improvement. But you don't have
    > to.
    >
    > --
    > Erik Wikström



    Thanks both for your answer, that solves my problem :)

    Best regards,

    N.
     
    , Apr 2, 2007
    #4
    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. thinktwice
    Replies:
    4
    Views:
    905
    =?iso-8859-1?q?Kirit_S=E6lensminde?=
    Sep 7, 2006
  2. Navaneeth
    Replies:
    8
    Views:
    692
    Keith Thompson
    Jan 1, 2011
  3. Navaneeth
    Replies:
    3
    Views:
    330
    Seebs
    Jan 1, 2011
  4. Navaneeth
    Replies:
    1
    Views:
    322
    Ben Bacarisse
    Jan 4, 2011
  5. Navaneeth
    Replies:
    3
    Views:
    335
    Thad Smith
    Jan 5, 2011
Loading...

Share This Page