How would I go about converting an integer to it's equivalent ASCII value?
Printable View
How would I go about converting an integer to it's equivalent ASCII value?
itoa() in stdlib.h ?
If you just want to convert a single digit (0-9), do this
char character=(char)(digit+48);
Does that return the string representation of a number (ie "65"), or for a value like 65 return the equivalent ASCII character, "A"?
All characters are represented as numbers in a computer -
char can be treated as a number (unsigned: 1-255) (signed -127 -127)or whatever the ASCII designation for the char is. Internally it is still a number.
And you can convert any integer that is <255 to char. Many character functions like getc() actually use integers. Not char.Code:char s;
for(s=65;s<91;s++) printf(" %c \n",s);
char and int are interchangeable for a lot of functions.
I think someone else had shown this piece of code here before.:DQuote:
Originally posted by xuralarux
Does that return the string representation of a number (ie "65"), or for a value like 65 return the equivalent ASCII character, "A"?
cout<<(int)('A')<<endl;//65
cout<<(char)(65)<<endl;//'A' character
[edit]Forget what I said here. I was tired.[/edit]
For a long number, it's sprintf (_itoa is an MS extension).
Ok, so really as long as you can get an int, you can just use a (char) cast to get the equivalent ASCII character... And here I was getting the idea that doing number <-> ASCII stuff was easier in VB! Now that I'm kinda getting the hang of C++, I don't think I wanna go back to Micro$haft stuff! Well, mebbe if a juicy position as a SQL DB admin popped up, but other than that... :rolleyes: :DQuote:
Originally posted by transcendental
I think someone else had shown this piece of code here before.:D
cout<<(int)('A')<<endl;//65
cout<<(char)(65)<<endl;//'A' character
BTW it's not + 48 but either + 0x41 or + 65 or (best) + 'A'.Quote:
Originally posted by CornedBee
BTW it's not + 48 but either + 0x48 or + 65 or (best) + 'A'.
For a long number, it's sprintf (_itoa is an MS extension).
Anyway I'm really referring to 48. 48 is the ascii no for '0' since the thread title is 'int to ASCII'.
Oh yeah, stupid me.
It was 2 AM...
Thanks for the help all, got the program done.