How to convert a char* to a BYTE* and back?
In C++, char is defined to be 1 byte. So saying sizeof returns number of chars or number of bytes is the same thing.
Quote:Original post by Alpha_ProgDesQuote:Original post by dbzprogrammervoid foo(){ char* charData = new char; *charData = 'h'; BYTE* byteData = new BYTE; *byteData = *(BYTE*)charData; //notice change}
Would that work? I'm at a computer with no compiler, so I can't tell...
even with typedefs sometimes you have to explicitly cast a variable. so for these variable one is of BYTE* and the other variable is of char*.
so here: (BYTE*)charData
that converts the original variable, which is char* charData, to BYTE* charData.
then: *(BYTE*)charData
now that the charData has been temporarily been converted to a BYTE* you can now use the dereference operator '*' to retrive the value.
i hope that clear. ask if you have any questions.
edit: tested it on VC++ EE with the Platform SDK and it works.
Wow, I can't believe I forgot to throw the C-cast in my code, sorry!
Thanks for all of your quick replys.
I'm trying to use a DPN_BUFFER_DESC (directPlay network buffer). I'm trying to fill it with data for a chat message. The members are pBufferData (BYTE*) and dwBufferSize (DWORD). Mayber there is something wrong with my code:
On another computer that receives the message, the message is added to a string that will be displayed.
Oh, the error happens on the recieving computer and comes up with the file of "strcat".
I'm trying to use a DPN_BUFFER_DESC (directPlay network buffer). I'm trying to fill it with data for a chat message. The members are pBufferData (BYTE*) and dwBufferSize (DWORD). Mayber there is something wrong with my code:
buffer.pBufferData = (BYTE*)m_chatMessage;buffer.dwBufferSize = sizeof( (BYTE*)m_chatMessage );
On another computer that receives the message, the message is added to a string that will be displayed.
strcat( m_final, (char*)buffer.pBufferData );
Oh, the error happens on the recieving computer and comes up with the file of "strcat".
You realize that you're only sending four bytes of your string with that right? Is that what you want?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement