How to convert a char* to a BYTE* and back?

Started by
15 comments, last by darkzim 18 years, 3 months ago
In C++, char is defined to be 1 byte. So saying sizeof returns number of chars or number of bytes is the same thing.
Advertisement
Quote:Original post by Alpha_ProgDes
Quote:Original post by dbzprogrammer
void 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!
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
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:
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".

---------------------------------darkzim

You realize that you're only sending four bytes of your string with that right? Is that what you want?
No I want to send the whole thing [help]. What am I doing wrong to send only 4 bytes?

---------------------------------darkzim

Think about it. What did you put down as the buffer size?
Is this right (plus one for null termination)?
buffer.dwBufferSize = (DWORD)(strlen(m_chatMessage)+1);

---------------------------------darkzim

This topic is closed to new replies.

Advertisement