What function and header file does one use to get the ascii value of a character in a string?
For example
char st1[16] = "Hello, world.";
int t = 'ascii value of' st1[4];
What function and header file does one use to get the ascii value of a character in a string?
For example
char st1[16] = "Hello, world.";
int t = 'ascii value of' st1[4];
Include iostream.h. You could also use printf(); if you prefer that.
To get the ASCII-value (in this case 111):
int t=st1[4];
cout << t;
To get the character (in this case the 'o'):
int t=st1[4];
cout << (char)t;
Last edited by Magos; 11-22-2001 at 12:17 PM.
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
That works great, thank you.
Now I was wondering how to put the ASCII value back into a string as a character.
Such as~
int a = 65;
char st[16];
st[16] = 'character representation of' (a);
Oh, well, why not?
Code:char Str[8]; //Create a string (array) Str[0]='A'; //Set element 0 to A Str[1]=45; //Set element 1 to ASCII-value 45
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
There is a simpler function that does this.
Ex:
#include <conio.h>
...............................
int ascii;
char ch;
ascii=getch();
This should do it.