present int as char
-
I am figuring out how to present single chars from integer value, that can be only from 65-122(ASCII). I randomly get int value and now i would like to change that int to char, so lets say for int value 65 output would be 'A'. With itoa(int value, buffer, 10) where buffer is char*[30] i get result from buffer in numbers. What can be used to convert this? Thanks
-
I am figuring out how to present single chars from integer value, that can be only from 65-122(ASCII). I randomly get int value and now i would like to change that int to char, so lets say for int value 65 output would be 'A'. With itoa(int value, buffer, 10) where buffer is char*[30] i get result from buffer in numbers. What can be used to convert this? Thanks
It could be:
int i(65);
// only the low byte will be accepted
char c = char(i);...or even:
int i(65);
// both bytes will be accepted
WCHAR wc = WCHAR(i);:)
Check your definition of Irrationality[^] :) 1 - Avicenna 5 - Hubbard 3 - Own definition
-
I am figuring out how to present single chars from integer value, that can be only from 65-122(ASCII). I randomly get int value and now i would like to change that int to char, so lets say for int value 65 output would be 'A'. With itoa(int value, buffer, 10) where buffer is char*[30] i get result from buffer in numbers. What can be used to convert this? Thanks
In addition to what Eugen said, you can do this -
char buffer[30];
sprintf_s(buffer, 30, "%c", int_value);«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) -
I am figuring out how to present single chars from integer value, that can be only from 65-122(ASCII). I randomly get int value and now i would like to change that int to char, so lets say for int value 65 output would be 'A'. With itoa(int value, buffer, 10) where buffer is char*[30] i get result from buffer in numbers. What can be used to convert this? Thanks
Casting would be the simple way of doing it. In C, casting looks like this:
(char)x
In C++, you can do the above or use static_cast which is the preferred method:
static_cast<char>(x)
This is uglier and takes more text, but it is safer (more type checking at compile time) and it is good to make a habit of it. Once you cast the int to a char, it will be treated as a char for all operations. Of course, it will be converted implicitly when assigning it to a char and casting will not be needed.
modified on Thursday, March 4, 2010 10:51 PM