Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to avoid reinterpret_cast in this snippet?

Reply
Thread Tools

how to avoid reinterpret_cast in this snippet?

 
 
KK
Guest
Posts: n/a
 
      12-01-2005
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;
}

 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      12-01-2005
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.
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      12-01-2005
* 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?
 
Reply With Quote
 
ben
Guest
Posts: n/a
 
      12-02-2005
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

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      12-02-2005
* 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?
 
Reply With Quote
 
Bob Hairgrove
Guest
Posts: n/a
 
      12-02-2005
On 1 Dec 2005 14:47:10 -0800, "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;
>}


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

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      12-02-2005
* Bob Hairgrove:
> On 1 Dec 2005 14:47:10 -0800, "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;
> >}

>
> 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?
 
Reply With Quote
 
Bob Hairgrove
Guest
Posts: n/a
 
      12-02-2005
On Fri, 02 Dec 2005 09:58:35 GMT, (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

 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      12-02-2005
Bob Hairgrove wrote:

> On Fri, 02 Dec 2005 09:58:35 GMT, (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
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      12-02-2005
* Bob Hairgrove:
> On Fri, 02 Dec 2005 09:58:35 GMT, (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?
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
reinterpret_cast<std::size_t>(p) and reinterpret_cast<std::size_t&>() Alex Vinokur C++ 1 02-06-2011 07:48 AM
Avoid having a SQL express for web parts and avoid personalization Roger23 ASP .Net 2 10-12-2006 10:54 PM
Avoid wasting time or how to avoid initialization Alexander Malkis C++ 8 04-13-2004 11:23 PM
reinterpret_cast - to interpret double as long Suzanne Vogel C++ 17 07-07-2003 02:50 PM



Advertisments