![]() |
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; } |
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. |
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? |
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 |
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? |
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 |
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? |
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 |
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 |
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.