Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Convert std::string to std::vector<unsigned char> and vice versa

Reply
Thread Tools

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

 
 
timor.super@gmail.com
Guest
Posts: n/a
 
      04-02-2007
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.

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      04-02-2007
* :
>
> 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?
 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      04-02-2007
On 2 Apr, 14:20, timor.su...@gmail.com 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

 
Reply With Quote
 
timor.super@gmail.com
Guest
Posts: n/a
 
      04-02-2007
On 2 avr, 15:37, "Erik Wikström" <eri...@student.chalmers.se> wrote:
> On 2 Apr, 14:20, timor.su...@gmail.com 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to convert narrow string to wide string and vice versa? thinktwice C++ 4 09-07-2006 10:55 AM
How to convert '1' into '-1' and vice versa? - Java 8 06-11-2005 09:28 AM
Why convert Word doc to PDF? Or vice versa Harry Computer Support 3 03-08-2005 12:44 PM
Convert a string to float and vice versa Flyingaway C++ 6 02-19-2005 11:08 PM
Convert Dictionary to String, vice versa? Byron Python 6 09-15-2004 11:55 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57