473,698 Members | 2,942 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
32 3112
* Bob Hairgrove:
On Mon, 05 Dec 2005 01:06:01 GMT, al***@start.no (Alf P. Steinbach)
wrote:
Try

bcc32 -D"_RWSTD_COMPIL E_INSTANTIATE" -w-ccc -w-rch test_uchar.cpp

Hth.,
No, it doesn't really "help" (HTH == "hope this helps") <g>

The basic question remains:
Is it *required* by the C++ standard that your code compiles (and
links)?


Yes, but not that it "works". The working aspect is covered by
conversion from unsigned char to char (not pointers but values). And
this value conversion, although in-practice well-defined on all modern
platforms, is formally undefined behavior language-wise.

To make more sure that it works, not just compiles and links, one would
have to include a compile time assertion like

static unsigned char const polyanna = 156; // Say.
BOOST_STATIC_AS SERT(
static_cast<uns igned char>( static_cast<cha r>( polyanna ) ) ==
polyanna
);

And add the assumption that the original data consists of char values
casted to unsigned char -- but I think that borders on the ludicrious.

And if you think so, please explain why (and not just: "it
does").
By definition of the templated std::basic_stri ng constructor, which is
required to copy the values.

[I hope you realize that the above "hack" can create serious problems
with code that is distributed among different DLLs which rely on
things defined in the DLLs supplied by Borland (e.g., anything even
remotely depending on VCL)?]


No, I didn't realize that, and I don't know that, and it isn't a hack,
it's a compiler invocation. It sounds improbable. But, it's an old
compiler and I don't really care about its undocumented features. ;-)

Hth.,

- 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 5 '05 #31
Alf P. Steinbach wrote:
* Ron Natalie:
Alf P. Steinbach wrote:
* 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.

It is correct.


Nope.

There is NO constructor that takes ONLY a unsigned char* (or even
an unsigned char* and a integer like the OP's original intended use).

Dec 6 '05 #32
* Ron Natalie:
Alf P. Steinbach wrote:
* Ron Natalie:
Alf P. Steinbach wrote:
* 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.
It is correct.


Nope.

There is NO constructor that takes ONLY a unsigned char* (or even
an unsigned char* and a integer like the OP's original intended use).


So?

--
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 6 '05 #33

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

Similar topics

17
12826
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 longs. I tried to use reinterpret_cast for this purpose, but it returned zero every time. double...
6
2792
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
4062
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
4871
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
2434
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 type and inserted in the array.
27
4335
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 ought to be a way to deal with these kinds of functions yet still retain some semblance of type...
2
2955
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
2677
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
8611
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9031
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8876
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6531
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4624
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
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
2
2341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.