Sign in to follow this  
patrickpp

Bug in Lesson 14 - text length calculation

Recommended Posts

patrickpp    122
Hello all, I think there is a bug in lesson 14 in the section that calculates the text length of a string. I discovered it because accented characters such as é, à, etc. are not displayed in the example program. In fact, all characters with ASCII code above 128 are not displayed. The reason is a mismatch between signed char and unsigned char Code of lesson 14: char text[256]; --> char data type defaults to signed char in VC ++ (From MSDN: char : –128 to 127) for (unsigned int loop=0;loop<(strlen(text));loop++) // Loop To Find Text Length { length+=gmf[text[loop]].gmfCellIncX; // Increase Length By Each Characters Width } --> for all ASCII codes above 128, text[loop] is a value below 0 --> gmf[text[loop]].gmfCellIncX gives undesired results For example : character "é" (ASCII code 233) --> text[loop] will have -23 as value because the char data type is in fact signed char. Solution: cast the char to an unsigned char unsigned char ch; for (unsigned int loop=0;loop<(strlen(text));loop++) // Loop To Find Text Length { ch = (unsigned char) text[loop]; length+=gmf[ch].gmfCellIncX; // Increase Length By Each Characters Width } --> Now ch will have the correct value 233, and the "é" will be nicely displayed Kind regards, Patrick Pasteels

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this