# Thread: Int 2 Char

1. ## Int 2 Char

I am new to C++, can anyone kindly tell me how I can transfer the value of an Int variable to a Char Variable.

If int X=55, I want char Z=55,
I have tried Z=(char)X;
but it doesn't works.

Please Help.

2. Well, what do you mean exactly? Do you want Z to equal the ASCII value 55 or Z to be a string "55"?

3. I'm pretty sure you would have to convert from an integer to a string, I think that is because integers are 2 bytes and characters are just 1 byte, I think.

There is a command called itoa, it stands for integer to alpha, I think it is a function in stdlib.h, have a look on your help.

This is just an idea but I think it should work, I haven't compiled it.

Code:
```#include <iostream.h>
#include <stdlib.h>

int main()
{
int x = 55;
char z[5];

cout<<x;

itoa(x, z, 5);

cout<<z;

return 0;
}```
you pass the itoa function the name of the integer variable the name of the string and the length of the string, I hope this works and helps.

4. Originally posted by UnclePunker
I'm pretty sure you would have to convert from an integer to a string, I think that is because integers are 2 bytes and characters are just 1 byte, I think.
It's because there are no letters in the range of integers. ASCII is a set of characters mapped to numbers. What the poster wants is to take the ASCII representation of those numbers. Actually stored much differently than the original.

"A" = 65
"2" = 50
"35" = 51, 53

etc...

5. ## Thanx

Thanx for your Mails.
You are right I do wish to convert the integer to string.
& I will try out your advise.

6. I guess your right about the ASCII values, but am I not right that integers are 2 bytes and characters are 1 byte, and I think he did actually want the z character variable to print out 55, I did a similar exercise when I had just started a few months back.

Popular pages Recent additions