Byte Indices

Unicode TasksMultibyte Character Set (MBCS) Tasks

Use the following tips:

  • Working with a bytewise index into a string presents problems similar to those posed by pointer manipulation. Consider this example, which scans a string for a backslash character:

    while ( rgch[ i ] != '\\' )
        i++;
    

    This may index a trail byte, not a lead byte, and thus it may not point to a character.

  • Use the _mbslen function to solve the preceding problem:

    while ( rgch[ i ] != '\\' )
        i += _mbslen( rgch + i );
    

    This correctly indexes to a lead byte, hence to a character. The _mbslen function determines the size of a character (one or two bytes).

See Also   The Last Character in a String