• Advertisement
Sign in to follow this  

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

This topic is 4439 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all. I'm having trouble trying to do the thing stated in the subject... I've tried to do byteData = (BYTE*)charData, but I'm assuming that doesn't work becuase it returns error messages when I try to handle the data. Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
That should work (unless you are trying to convert numbers to text). What is the error? Post the code that causes the error.

Share this post


Link to post
Share on other sites

void foo()
{
char* charData = new char;
*charData = 'h';
BYTE* byteData = new BYTE;
*byteData = *charData;
}



Would that work? I'm at a computer with no compiler, so I can't tell...

Share this post


Link to post
Share on other sites
an unsigned char is one byte long. char is more I think. you will lose data going char->byte and waste data going byte->char.

(I think)

Share this post


Link to post
Share on other sites
arbuckle: char and unsigned char are typically a byte. The only difference is the interpretation of the bits.

dbzprogrammer: No. You leaked the byte you allocated to byteData. Assigning pointers doesn't copy memory. EDIT: I take it back, misread your code. :)

darkzim: JohnBolton is right, need more information to tell you what's wrong.

Share this post


Link to post
Share on other sites
Quote:
Original post by arbuckle911
an unsigned char is one byte long. char is more I think.

sizeof(char) = sizeof(unsigned char) = sizeof(signed char) = 1 *edit: And sizeof returns the size in bytes. So a char always takes up one byte, no matter how large that may be.

char can be either signed or unsigned, depending on the compiler. Either way, it is a distinct type from both unsigned char and signed char.

CM

Share this post


Link to post
Share on other sites
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.

[Edited by - Alpha_ProgDes on December 31, 2005 1:13:53 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Conner McCloud
Quote:
Original post by arbuckle911
an unsigned char is one byte long. char is more I think.

sizeof(char) = sizeof(unsigned char) = sizeof(signed char) = 1 *edit: And sizeof returns the size in bytes. So a char always takes up one byte, no matter how large that may be.

char can be either signed or unsigned, depending on the compiler. Either way, it is a distinct type from both unsigned char and signed char.

CM

Actually, doens't sizeof return the number of chars that can fit in the variable, e.g. sizeof(char) is always 1?

Share this post


Link to post
Share on other sites
In C++, char is defined to be 1 byte. So saying sizeof returns number of chars or number of bytes is the same thing.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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".

Share this post


Link to post
Share on other sites
You realize that you're only sending four bytes of your string with that right? Is that what you want?

Share this post


Link to post
Share on other sites
No I want to send the whole thing [help]. What am I doing wrong to send only 4 bytes?

Share this post


Link to post
Share on other sites
Think about it. What did you put down as the buffer size?

Share this post


Link to post
Share on other sites
Is this right (plus one for null termination)?

buffer.dwBufferSize = (DWORD)(strlen(m_chatMessage)+1);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement