Jump to content
  • Advertisement
Sign in to follow this  
darkzim

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

This topic is 4707 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

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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!