Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting fron int to char

Status
Not open for further replies.

atoledo

Programmer
Oct 14, 2002
16
ES
How can I convert a int to a single char?

Thank you!
 
Well you can’t “convert” it since the size is different. An int is larger than one byte so if you perform a cast you just get the low byte value. If you want a different byte you would need to do a bitwise AND in concert with a bitwise RIGHT SHIFT to accomplish it.

Code:
int n = 500;
char x = (char)n; // gets the low byte
char y = ((n & 0xff00) >> 8); // gets the second lowest byte

-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top