   C++ and Code Page 437
  Hi there,I'm using a bitmap font in my application which is based upon the IBM code page 437 (http://en.wikipedia.org/wiki/Code_page_437).I'm having some trouble getting it to display the correct characters, however. Characters 0-127 display fine, but anything over that always displays an incorrect glyph. In most cases, the glyph that is selected by the program to display is always 1 line above or below the character I actually do want to display.I'm using strings in my bitmap font parser instead of char*s but I haven't noticed a different between either of those methods.A char in C++ is, to my understanding, in the range of 0-255. However, I can't figure out if there is some problem with my parser or perhaps the compiler (VC9). It doesn't make sense that characters over 127 would be shifted in either context.Here is my parser. Tell me if you see any problems with it or have any other ideas. It has been designed to take input such as /b02 and /f12 and change the background and foreground colors based on those codes.void Text::write( string input, int x, int y ){row = 0;line = 0;glMatrixMode( GL_PROJECTION );glLoadIdentity();glMatrixMode( GL_MODELVIEW );glLoadIdentity();glDisable( GL_BLEND );glDisable( GL_TEXTURE_2D );gluOrtho2D( 0, 640, 400, 0 );modifier = 0;palette.set_color( 00 );for( int a = 0; a < (int)input.size(); a++ ){if(input.at(a) == 47 && input.at( a + 1 ) == 98 ){string foreground = input.substr( a + 2, 2 );palette.set_color( atoi( foreground.c_str() ) );modifier += 4;a += 3;}else if( input.at(a) == 47 && input.at( a + 1 ) == 102 ){modifier += 4;a += 3;}else{vx1 = x * char_width + ( a - modifier ) * char_width;vx2 = vx1 + char_width;vy1 = y * char_height;vy2 = vy1 + char_height;glBegin( GL_QUADS );glVertex2f( vx1, vy1 );glVertex2f( vx1, vy2 );glVertex2f( vx2, vy2 );glVertex2f( vx2, vy1 );glEnd();}}glMatrixMode( GL_PROJECTION );glLoadIdentity();glMatrixMode( GL_MODELVIEW );glLoadIdentity();glEnable( GL_BLEND );glEnable( GL_TEXTURE_2D );glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );gluOrtho2D( 0, 640, 400, 0 );glBindTexture( GL_TEXTURE_2D, font.get_texture() );glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );modifier = 0;palette.set_color( 42 );for( int a = 0; a < (int)input.size(); a++ ){if(input.at(a) == 47 && input.at( a + 1 ) == 102 ){string foreground = input.substr( a + 2, 2 );palette.set_color( atoi( foreground.c_str() ) );modifier += 4;a += 3;}else if( input.at(a) == 47 && input.at( a + 1 ) == 98 ){modifier += 4;a += 3;}else{row = input.at(a) % 16;line = input.at(a) / 16;tx1 = row * char_width / font.get_width();tx2 = ( tx1 * font.get_width() + char_width ) / font.get_width();ty1 = line * char_height / font.get_height();ty2 = ( ty1 * font.get_height() + char_height ) / font.get_height();vx1 = x * char_width + ( a - modifier ) * char_width;vx2 = vx1 + char_width;vy1 = y * char_height;vy2 = vy1 + char_height;glBegin( GL_QUADS );glTexCoord2f( tx1, ty1 ); glVertex2f( vx1, vy1 );glTexCoord2f( tx1, ty2 ); glVertex2f( vx1, vy2 );glTexCoord2f( tx2, ty2 ); glVertex2f( vx2, vy2 );glTexCoord2f( tx2, ty1 ); glVertex2f( vx2, vy1 );glEnd();}}}
  There are two potential problems that I can immediately think of.Are you sure you have the characters are encoded with the correct values? A character you want to draw may appear correct in, say, the editor, but it may not correspond to the same glyph in your character map.Arithmetics on the character codes may not behave as you expect. For example;row = input.at(a) % 16;line = input.at(a) / 16;If the compiler treats char as a signed value, which Visual C++ does by default, arithmetics on it will be unsigned also. Especially the remainder-operator may give you unexpected results. Cast the result to an unsigned value the first thing you do.;  I tried this but it didn't work:row = unsigned( input.at(a) % 16 );line = unsigned( input.at(a) / 16 );How can I make the result unsigned like you said?For reference, here's my Text class:class Text{private:Texture font;GLfloat char_width;GLfloat char_height;Palette palette;int row;int line;int modifier;GLfloat vx1;GLfloat vx2;GLfloat vy1;GLfloat vy2;GLfloat tx1;GLfloat tx2;GLfloat ty1;GLfloat ty2;public:Text::Text();Text::Text( char* path );Text::~Text();void Text::load_texture( char* path );GLuint Text::get_texture();void Text::write( string input, int x, int y );void Text::draw( char input, int x, int y, int background, int foreground );};;  Use unsigned char to represent values in the range 0~255. You can either modify your methods to use them directly, or cast where appropriate (i.e. before performing any operation on them that may yield a signed result).I'm afraid I'm not familiar with the nuances of C++'s casting, butrow = ((unsigned char)input.at(a)) % 16;would be how I'd do it in C.   