Converting from int to char

Oct 29, 2012 at 3:10am
I have an int of 3483498. When I cast it a char, it displays the char 'j'. When I convert back, it is 20. The ASCII for 20 is not even 'j'. I am utterly lost. How do I get back 3483498?

1
2
3
4
5
int D = 3483498;
char a = char(D);
cout << a << endl;
int b = int(a);
cout << b << endl;
Oct 29, 2012 at 4:08am
When I convert back, it is 20.

That's odd, I tried it and got 106, which is what it should be.

How do I get back 3483498?

You can't go back once you cast a type like int to a type that is smaller. You lose the rest of in the important bits.

It's like rounding a decimal to the nearest integer. If I gave you a number that I rounded beforehand, you wouldn't be able to tell me (for sure) what the tenths place was.
Oct 29, 2012 at 4:18am
Look up the atoi() (Character to integer) and itoa() (Integer to character) functions. They should be able to help you.
Oct 29, 2012 at 5:18am
atoi() and itoa() won't help you too.. you should use stringstream for you purpose..
Oct 29, 2012 at 3:42pm
I tried using itoa(), but it didn't help either. In the end, I used stringstream. It works out much better.
Topic archived. No new replies allowed.