How to convert a char* to a BYTE* and back?
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.
That should work (unless you are trying to convert numbers to text). What is the error? Post the code that causes the error.
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...
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)
(I think)
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.
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.
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
Quote: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.
[Edited by - Alpha_ProgDes on December 31, 2005 1:13:53 PM]
Quote:Original post by Conner McCloudQuote: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?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement