Results 1 to 11 of 11

Thread: How to get ASCII value from String class?

  1. #1

    Thread Starter
    Addicted Member ChuckB's Avatar
    Join Date
    Jul 2002
    Location
    South Carolina, USA
    Posts
    157

    How to get ASCII value from String class?

    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?

    Thanks,
    ChuckB
    I learn Robotics, Electronics and Game Programming at http://www.gameinstitute.com

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    What do you want to do when you find the ASCII number?
    Code:
    for(string::iterator i = str.begin(); i != str.end(); ++i)
    {
      if(*i == 65)
        cout << "Found an A at index " << (i-str.begin()) << endl;
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Addicted Member ChuckB's Avatar
    Join Date
    Jul 2002
    Location
    South Carolina, USA
    Posts
    157
    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.
    I learn Robotics, Electronics and Game Programming at http://www.gameinstitute.com

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Use string::const_iterator instead of string::iterator, then you don't need to copy the string.

    You have 70 characters? This is a static number, isn't it? You should use a static array instead of the vector then.

    Or even better (as it would speed things up) you could use an STL map:
    Code:
    #include <map>
    
    struct CHARACTER {
      POINT a;
      DIMENSION b;
    };
    
    typedef std::map<char, CHARACTER>  font_map;
    typedef std::map<char, CHARACTER>::const_iterator font_map_const_iter;
    typedef std::map<char, CHARACTER>::value_type font_map_value;
    
    font_map characters;
    // Needs code to fill the map.
    // The map is filled like this:
    void addEntry(char code, const CHARACTER &ch) {
      character.insert(font_map_value(code, ch));
    }
    
    void DrawText2D(int x, int y, int size, const string& text ){
      glEnable(GL_TEXTURE_2D);
      glBindTexture ( GL_TEXTURE_2D, texture_objects[TEX_FONT1]);
      glBegin(GL_QUADS);
    
      for(string::const_iterator m = text.begin(); m != text.end(); ++m)
      {
        font_map_const_iter pch = characters.find(*m);
        glTexCoord2f(pch->second->a.x, pch->second->a.y);
        glVertex2f(x + step * size, y );
    
        . . . etc.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Addicted Member ChuckB's Avatar
    Join Date
    Jul 2002
    Location
    South Carolina, USA
    Posts
    157
    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.

    Thanks again.
    ChuckB
    I learn Robotics, Electronics and Game Programming at http://www.gameinstitute.com

  6. #6

    Thread Starter
    Addicted Member ChuckB's Avatar
    Join Date
    Jul 2002
    Location
    South Carolina, USA
    Posts
    157
    CornedBee,

    Will you give me some feedback on this...

    Code:
      ostringstream Str;
    
      . . . .
    
      for(string::const_iterator m = text.begin(); m != text.end(); ++m)
      {
        Str << *m;
        string mynum(Str.str());
        LogData(mynum);
    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
    I learn Robotics, Electronics and Game Programming at http://www.gameinstitute.com

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.
    Code:
      ostringstream Str;
    
      . . . .
    
      for(string::const_iterator m = text.begin(); m != text.end(); ++m)
      {
        Str << (int)*m << ' ';
        string mynum(Str.str());
        LogData(mynum);
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    When using a map<>, make sure to test for a return value of yourmap.end(), since it may not be found.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Right.
    if(pch == characters.end())
    // not found
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10

    Thread Starter
    Addicted Member ChuckB's Avatar
    Join Date
    Jul 2002
    Location
    South Carolina, USA
    Posts
    157
    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. :-)
    Attached Images Attached Images  
    I learn Robotics, Electronics and Game Programming at http://www.gameinstitute.com

  11. #11

    Thread Starter
    Addicted Member ChuckB's Avatar
    Join Date
    Jul 2002
    Location
    South Carolina, USA
    Posts
    157
    Oh Yeah! The spacing in the text in the above graphic was intentional experimentation. :-)
    I learn Robotics, Electronics and Game Programming at http://www.gameinstitute.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width