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

Started by
15 comments, last by darkzim 18 years, 3 months ago
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.

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

Advertisement
That should work (unless you are trying to convert numbers to text). What is the error? Post the code that causes the error.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
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...
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
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)
hm, or maybe since it's a pointer it wouldn't matter.
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.
Orin Tresnjak | Graphics ProgrammerBethesda Game StudiosStandard Disclaimer: My posts represent my opinions and not those of Bethesda/Zenimax, etc.
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
A char, an unsigned char and a byte are one byte each.
"C lets you shoot yourself in the foot rather easily. C++ allows you to reuse the bullet!"
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]

Beginner in Game Development?  Read here. And read here.

 

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?

This topic is closed to new replies.

Advertisement