473,569 Members | 2,562 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to avoid reinterpret_cas t 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_cas t operator?
Or Is there a simple way to get around this?
Thanks.
-KK
/* not tested yet */
typedef unsigned char BYTE
std::string GetStringFromBy teBuffer(const BYTE* const buffer, int pos )
{
const char *chAry = reinterpret_cas t <const char *> (buffer + pos);
std::string tmp(chAry,12);
return chAry;
}

Dec 1 '05 #1
32 3084
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_cas t operator?
Or Is there a simple way to get around this?
Thanks.
-KK
/* not tested yet */
typedef unsigned char BYTE
std::string GetStringFromBy teBuffer(const BYTE* const buffer, int pos )
{
const char *chAry = reinterpret_cas t <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.
Dec 1 '05 #2
* 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_cas t operator?
Or Is there a simple way to get around this?
Thanks.
-KK
/* not tested yet */
typedef unsigned char BYTE
std::string GetStringFromBy teBuffer(const BYTE* const buffer, int pos )
{
const char *chAry = reinterpret_cas t <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?
Dec 1 '05 #3
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_cas t operator?
Or Is there a simple way to get around this?
Thanks.
-KK
/* not tested yet */
typedef unsigned char BYTE
std::string GetStringFromBy teBuffer(const BYTE* const buffer, int pos )
{
const char *chAry = reinterpret_cas t <const char *> (buffer + pos);
std::string tmp(chAry,12);
return chAry;
}

In addition to Alf's suggestion, here is another choice:

void GetStringFromBy teBuffer(const BYTE* const buffer,
std::string& s)
{
std::copy(buffe r, buffer+12, s.begin());
}

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

GetStringFromBy teBuffer(
buff + pos,
str);

Ben

Dec 2 '05 #4
* 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_cas t operator?
Or Is there a simple way to get around this?
Thanks.
-KK
/* not tested yet */
typedef unsigned char BYTE
std::string GetStringFromBy teBuffer(const BYTE* const buffer, int pos )
{
const char *chAry = reinterpret_cas t <const char *> (buffer + pos);
std::string tmp(chAry,12);
return chAry;
}
In addition to Alf's suggestion, here is another choice:

void GetStringFromBy teBuffer(const BYTE* const buffer,
std::string& s)
{
std::copy(buffe r, 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... ;-)

GetStringFromBy teBuffer(
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?
Dec 2 '05 #5
On 1 Dec 2005 14:47:10 -0800, "KK" <ke*******@yaho o.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_cas t operator?
Or Is there a simple way to get around this?
Thanks.
-KK
/* not tested yet */
typedef unsigned char BYTE
std::string GetStringFromBy teBuffer(const BYTE* const buffer, int pos )
{
const char *chAry = reinterpret_cas t <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_cas t, as you have done. There is really
no way to avoid it if you must pass unsigned char to this function.

--
Bob Hairgrove
No**********@Ho me.com
Dec 2 '05 #6
* Bob Hairgrove:
On 1 Dec 2005 14:47:10 -0800, "KK" <ke*******@yaho o.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_cas t operator?
Or Is there a simple way to get around this?
Thanks.
-KK
/* not tested yet */
typedef unsigned char BYTE
std::string GetStringFromBy teBuffer(const BYTE* const buffer, int pos )
{
const char *chAry = reinterpret_cas t <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_cas t, 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?
Dec 2 '05 #7
On Fri, 02 Dec 2005 09:58:35 GMT, al***@start.no (Alf P. Steinbach)
wrote:
Since char and unsigned char are different types, you must use either
a C-style cast or reinterpret_cas t, 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
No**********@Ho me.com
Dec 2 '05 #8
Bob Hairgrove wrote:
On Fri, 02 Dec 2005 09:58:35 GMT, al***@start.no (Alf P. Steinbach)
wrote:
Since char and unsigned char are different types, you must use either
a C-style cast or reinterpret_cas t, 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(In putIterator begin, InputIterator end,
const Allocator& a = Allocator());
Since unsigned char is convertible to char, this constructor should match.
Best

Kai-Uwe Bux
Dec 2 '05 #9
* Bob Hairgrove:
On Fri, 02 Dec 2005 09:58:35 GMT, al***@start.no (Alf P. Steinbach)
wrote:
Since char and unsigned char are different types, you must use either
a C-style cast or reinterpret_cas t, 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?
Dec 2 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
12788
by: Suzanne Vogel | last post by:
I'd like to convert a double to a binary representation. I can use the "&" bit operation with a bit mask to convert *non* float types to binary representations, but I can't use "&" on doubles. To get around this limitation on double, I'd like to keep the bits of the double the *same* but change its interpretation to long. I can use "&" on...
6
2784
by: hack_tick | last post by:
hi there i m trying to convert (viod *) data to (unsigned char) using reinterpret_cast as following void *aData; // some data already provided unsigned char c = reinterpret_cast<unsingned char>(aData); and the gcc is giving me the following error
5
4032
by: Christopher Benson-Manica | last post by:
enum foo {a,b,c,d}; int main() { unsigned int bar=reinterpret_cast< unsigned int >( a ); return bar; } g++ rejects the code based on the reinterpret_cast, so I presume it is not legal. Why not? And why should g++ accept the code when
4
4848
by: Alex Vinokur | last post by:
Should 'delete below (after casting to void*)' work fine? ------------------------------------------ char* p1 = new (nothrow) char; if (p1 == 0) return; void* p2 = reinterpret_cast<void*> (p1); delete p2; ------------------------------------------ Alex Vinokur
2
2423
by: Taran | last post by:
Hi All, I was trying some code which essentially does what the function 'func' here does. To simplify the things and make a consise post I have created a dummy app to show my problem. The issue was to assign a value to an array of pointers. The selection of array depends upon a condition and the void* ptr has to be casted to the correct...
27
4296
by: Noah Roberts | last post by:
What steps do people take to make sure that when dealing with C API callback functions that you do the appropriate reinterpret_cast<>? For instance, today I ran into a situation in which the wrong type was the target of a cast. Of course with a reinterpret_cast nothing complains until the UB bites you in the ass. It seems to me that there...
2
2945
by: jasm | last post by:
hello everybody! I have used reinterpret_cast for interpret a class object as a char*. This is the object: template<class T> class Coordinates { public: T *x; T *y;
2
2662
by: ciccio | last post by:
Hi, I was wondering what the main reason is why reinterpret_cast fails to work as expected when using optimizations. Here is the simple example code which fail to give the correct result when optimizing. #include <iostream> int main() { long D1 = 0xbcfbc4f0d9b65179; long D2 = 0xbcfbc4f0d9b65042;
0
7701
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7615
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8130
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7677
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3643
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1223
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.