* ben:
> KK wrote:
> > Hello all,
> > I have a unsigned char buffer 'buffer[1024]' and I need to convert the
> > first 12 bytes of it into a string. Below is a code that should work,
> > however, how can I avoid reinterpret_cast operator?
> > Or Is there a simple way to get around this?
> > Thanks.
> > -KK
> > /* not tested yet */
> > typedef unsigned char BYTE
> > std::string GetStringFromByteBuffer(const BYTE* const buffer, int pos )
> > {
> > const char *chAry = reinterpret_cast <const char *> (buffer + pos);
> > std::string tmp(chAry,12);
> > return chAry;
> > }
> >
>
>
> In addition to Alf's suggestion, here is another choice:
>
> void GetStringFromByteBuffer(const BYTE* const buffer,
> std::string& s)
> {
> std::copy(buffer, buffer+12, s.begin());
Nitpick: that assumes the string s passed as actual argument has size
12.
I'd write
s.assign( buffer, buffer+12 );
> }
>
> BYTE buff[19];
> int pos = 6;
> std::string str;
Oops...
> GetStringFromByteBuffer(
> buff + pos,
> str);
I think there's too much apparent magic in the standard library, so it's
too easy to think a standard algorithm can do no wrong...
--
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?