[program-l] Re: c++ byte vector to string conversion

  • From: Soronel Haetir <soronel.haetir@xxxxxxxxx>
  • To: program-l@xxxxxxxxxxxxx
  • Date: Mon, 19 Mar 2012 10:07:57 -0800

Actually, std::string need not be null terminated internally. (It can
in fact store sequences with embedded NUL characters).  An
implementation is of course free to tack a NUL character on the end,
and that does make implementing std::basic_string<>::c_str() quite a
bit easier, but it's no requirement of the standard to do so.

On 3/19/12, Joseph Lee <joseph.lee22590@xxxxxxxxx> wrote:
> Hi,
> That's correct - a string is a special case of a dynamic array -
> in this case, a dynamic array of characters.  Internally, strings
> are stored as c-strings (a NULL terminated array of characters)
> which can be initialized to either an empty string, some literals
> (some string) or a copy of another.  This constructor principle
> can be applied to unsigned character vector, which "emulates"
> strings.  Also, since strings are dynamic arrays (which vectors
> are also known), you can add and remove characters from the back,
> hence my algorithm on vector traversal and pushing back each
> character to the end of the string.
> As for character pointer (rather, dereferencing them), if you
> know the address of the first letter of the string, then you can
> use pointer arithmetic to get to the required address and find
> out wh't lives there (but that's getting too technical now).  For
> now, I think our suggestions would suffice for Tim's scenario.
> Cheers,
> Joseph
>  ----- Original Message -----
> From: Mark Long <markalong64@xxxxxxxxxxx
> To: <program-l@xxxxxxxxxxxxx
> Date sent: Mon, 19 Mar 2012 17:13:50 +0000
> Subject: [program-l] Re: c++ byte vector to string conversion
>
>
> Hi Lubos
> Unless I have misunderstand, the constructor of the string class
> does just that.
> Cheers
> Mark
>
>  Date: Mon, 19 Mar 2012 15:32:44 +0100
>  From: lubos.pintes@xxxxxxxxx
>  To: program-l@xxxxxxxxxxxxx
>  Subject: [program-l] Re: c++ byte vector to string conversion
>
>  But he needs to convert vector<unsigned char> to string, at
> least this
>  is how I understood his question.
>  Does this mean that it is possible to typecast vector.begin() to
> (char *)?
>  Dňa 19.  3.  2012 13:08, Mark Long  wrote / napísal(a):
>  One of the constructor overloads for the string class is *char
> so you
>  just need to pass the char* in when you create the string
> object.
>
>  Mark
>
>  Date: Mon, 19 Mar 2012 11:58:51 +0100
>  From: lubos.pintes@xxxxxxxxx
>  To: program-l@xxxxxxxxxxxxx
>  Subject: [program-l] Re: c++ byte vector to string conversion
>
>  I would try something like this:
>  #include <algorithm
>  Assume v is a vector<unsigned char> and s is a string
>  then algorithm
>  copy(v.begin(), v.end(), s.begin());
>  could do this.  Note that I didn't test this.  It may be
> necessary to set
>  a capacity of string s to match size of vector v.
>  Dňa 19.  3.  2012 1:16, Tim Burgess wrote / napísal(a):
>
>  Hi,
>
>  I have a library call that returns a std::vector <unsigned
> char>,
>  which I need to bring into an s td::string - does anybody have
> an
>  algorithm, please?
>
>  Best wishes.
>
>  Tim Burgess
>
>  Raised Bar Ltd
>
>  Phone: +44 (0)1827 719822
>
>  Don't forget to vote for improved access to music and music
>  technology at
>
>  http://www.raisedbar.net/petition.htm
>
>
>  ** To leave the list, click on the immediately-following link:-
>  ** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
>  ** If this link doesn't work then send a message to:
>  ** program-l-request@xxxxxxxxxxxxx
>  ** and in the Subject line type
>  ** unsubscribe
>  ** For other list commands such as vacation mode, click on the
>  ** immediately-following link:-
>  ** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
>  ** or send a message, to
>  ** program-l-request@xxxxxxxxxxxxx wit h the Subject:- faq
>
>  ** To leave the list, click on the immediately-following link:-
>  ** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
>  ** If this link doesn't work then send a message to:
>  ** program-l-request@xxxxxxxxxxxxx
>  ** and in the Subject line type
>  ** unsubscribe
>  ** For other list commands such as vacation mode, click on the
>  ** immediately-following link:-
>  ** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
>  ** or send a message, to
>  ** program-l-request@xxxxxxxxxxxxx with the Subject:- faq
>
>
> ** To leave the list, click on the immediately-following link:-
> ** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
> ** If this link doesn't work then send a message to:
> ** program-l-request@xxxxxxxxxxxxx
> ** and in the Subject line type
> ** unsubscribe
> ** For other list commands such as vacation mode, click on the
> ** immediately-following link:-
> ** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
> ** or send a message, to
> ** program-l-request@xxxxxxxxxxxxx with the Subject:- faq
>


-- 
Soronel Haetir
soronel.haetir@xxxxxxxxx
** To leave the list, click on the immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
** If this link doesn't work then send a message to:
** program-l-request@xxxxxxxxxxxxx
** and in the Subject line type
** unsubscribe
** For other list commands such as vacation mode, click on the
** immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
** or send a message, to
** program-l-request@xxxxxxxxxxxxx with the Subject:- faq

Other related posts: