|
-
July 14th, 2006, 01:28 PM
#1
reinterpret_cast question
Hi,
I have been reading in a lot of places that using reinterpret_cast is bad but they never explain the reason for it. I use it a lot in my code to convert int to char * since I need to send the individual bytes of the int across a network.
<CODE>
char *byteCrcPtr = NULL;
DWORD byteCrc = 0xFF23F2e4;
byteCrcPtr = reinterpret_cast<char *>(&byteCrc);
for(int i=0;i<sizeof(DWORD);i++) {
packet[i]= byteCrcPtr[i];
}
</CODE>
To convert back
<CODE>
DWORD byteCrc = *(reinterpret_cast<DWORD*>(&packet[9]));
</CODE>
I especially want to understand why this is bad and is there a better way to do this. Thanks
Amish
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|