Hi,
I have a string with text. I would like to iterate through the string and compare each character to an ASCII number. I cannot find a very clear answer....the only things I can think up requre multiple lines of code.
Code:
string text="Good morning";
for(int i = 0; i< text.length(); i++){
// here is where I need to code to extract a single character
// using substr and getting the ASCII value.
}
I would like to avoid using char arrays. Is there a nice ANSI C++ way of doing this?
Hi CornedBee,
Thanks for the reply. I have a bitmap image with 70 characters of a particular font mapped out on it. I have stored the exact coordinates of these 70 characters in a STL vector So I have a global 'vector <FONT> font'. There exists a font[z].ascii that holds the ascii number (int) of the corresponding font.
I want to read one character at a time from a string 'text' and look in the font[].ascii for a match.
I added your code in mine below. I substituted the 'm' for your 'i'. Also, I got errors manipulating 'text' directly so I had to place the value into another string 'onestr'. No more errors. However, I do not think this code is working correctly....
Any more tips would be helpful.
Regards,
ChuckB
Code:
void DrawText2D(int x, int y, int size, const string& text ){
string onestr=text;
glEnable(GL_TEXTURE_2D);
glBindTexture ( GL_TEXTURE_2D, texture_objects[TEX_FONT1]);
glBegin(GL_QUADS);
for(string::iterator m = onestr.begin(); m != onestr.end(); ++m)
{
for (int z=1;z < 70; z++){ //scroll through font vector looking for match
if( *m == font[z].ascii){
glTexCoord2f(font[z].a.x, font[z].a.y);
glVertex2f(x + step * size, y );
. . . etc.
Wham! I feel like I just gut hit by a Mack truck! :-)
Corned Bee, thanks, but that is a bit too much for me to absorb at this instant. I have been stuck on vectors after I found out about them. I guess I need to expand my knowledge of STL. Maybe soon.
I will apply the const_iterator to my code to get rid of that one extra string variable. I am copying this last example of yours to print out...and study later on.
The LogData is a function I wrote that receives a string. I make 'text' above = "Good Morning". From what you have written, doesn't '*m' have the ASCII value of the character being examinged? My little code converts the integer *m into a string for writing to a file.
I was really expecting to see ASCII numbers.
Regards,
ChuckB
The data type of the iterator dereferenced is still char, not int. An inserting a character into a stream means that it is interpreted as character. The solution is to cast it.
Corned Bee,
Thanks a bunch! The casting of (int)*m was right on and got me going. Thanks. I am attaching a screenshot showing the bitmap font...thanks to you for the final fix. Your code and final clue helped me scroll through my font data to match the ASCII value. :-)