Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   how to avoid reinterpret_cast in this snippet? (http://www.velocityreviews.com/forums/t450212-how-to-avoid-reinterpret_cast-in-this-snippet.html)

KK 12-01-2005 10:47 PM

how to avoid reinterpret_cast in this snippet?
 
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;
}


Ron Natalie 12-01-2005 10:52 PM

Re: how to avoid reinterpret_cast in this snippet?
 
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;
> }
>

You can't. unsigned char* and char* are not convertible. Even on
systems where char is inherently unsigned it's a distinct type. The
cast however should be safe.

Alf P. Steinbach 12-01-2005 11:04 PM

Re: how to avoid reinterpret_cast in this snippet?
 
* KK:
> 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;
> }


How about

std::string string12From( const BYTE buffer[], int pos )
{
return std::string( buffer+pos, buffer+pos+12 );
}

Btw., it's not a good idea to bury magic numbers like 12 in the code.

--
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?

ben 12-02-2005 12:41 AM

Re: how to avoid reinterpret_cast in this snippet?
 
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());
}

BYTE buff[19];
int pos = 6;
std::string str;

GetStringFromByteBuffer(
buff + pos,
str);

Ben


Alf P. Steinbach 12-02-2005 01:30 AM

Re: how to avoid reinterpret_cast in this snippet?
 
* 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?

Bob Hairgrove 12-02-2005 09:20 AM

Re: how to avoid reinterpret_cast in this snippet?
 
On 1 Dec 2005 14:47:10 -0800, "KK" <kewlkarun@yahoo.com> 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;
>}


Since char and unsigned char are different types, you must use either
a C-style cast or reinterpret_cast, as you have done. There is really
no way to avoid it if you must pass unsigned char to this function.

--
Bob Hairgrove
NoSpamPlease@Home.com

Alf P. Steinbach 12-02-2005 09:58 AM

Re: how to avoid reinterpret_cast in this snippet?
 
* Bob Hairgrove:
> On 1 Dec 2005 14:47:10 -0800, "KK" <kewlkarun@yahoo.com> 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;
> >}

>
> Since char and unsigned char are different types, you must use either
> a C-style cast or reinterpret_cast, as you have done. There is really
> no way to avoid it if you must pass unsigned char to this function.


You're the second person to state that so I'm interesting in the
reeasoning.

--
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?

Bob Hairgrove 12-02-2005 10:07 AM

Re: how to avoid reinterpret_cast in this snippet?
 
On Fri, 02 Dec 2005 09:58:35 GMT, alfps@start.no (Alf P. Steinbach)
wrote:

>> Since char and unsigned char are different types, you must use either
>> a C-style cast or reinterpret_cast, as you have done. There is really
>> no way to avoid it if you must pass unsigned char to this function.

>
>You're the second person to state that so I'm interesting in the
>reeasoning.


The reason? Because std::string has no constructor that takes unsigned
char* as an argument.

(And I believe that there are more than two others by now... ;)

--
Bob Hairgrove
NoSpamPlease@Home.com

Kai-Uwe Bux 12-02-2005 10:31 AM

Re: how to avoid reinterpret_cast in this snippet?
 
Bob Hairgrove wrote:

> On Fri, 02 Dec 2005 09:58:35 GMT, alfps@start.no (Alf P. Steinbach)
> wrote:
>
>>> Since char and unsigned char are different types, you must use either
>>> a C-style cast or reinterpret_cast, as you have done. There is really
>>> no way to avoid it if you must pass unsigned char to this function.

>>
>>You're the second person to state that so I'm interesting in the
>>reeasoning.

>
> The reason? Because std::string has no constructor that takes unsigned
> char* as an argument.


Hm, std::string has a templated constructor:


template<class InputIterator>
basic_string(InputIterator begin, InputIterator end,
const Allocator& a = Allocator());


Since unsigned char is convertible to char, this constructor should match.


Best

Kai-Uwe Bux

Alf P. Steinbach 12-02-2005 11:04 AM

Re: how to avoid reinterpret_cast in this snippet?
 
* Bob Hairgrove:
> On Fri, 02 Dec 2005 09:58:35 GMT, alfps@start.no (Alf P. Steinbach)
> wrote:
>
> >> Since char and unsigned char are different types, you must use either
> >> a C-style cast or reinterpret_cast, as you have done. There is really
> >> no way to avoid it if you must pass unsigned char to this function.

> >
> >You're the second person to state that so I'm interesting in the
> >reeasoning.

>
> The reason? Because std::string has no constructor that takes unsigned
> char* as an argument.


That is incorrect.

> (And I believe that there are more than two others by now... ;)


That is also incorrect.

Cheers,

- Alf

--
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?


All times are GMT. The time now is 11:59 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


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