views:

120

answers:

1

Can someone please assist me with converting special characters to something that can be correctly represented in an RTF file?

I am taking text stored in a string on the iPad and outputting it as an RTF file using NSASCIIStringEncoding. So far so good. What I've neglected to do successfully, is take into account special characters (e.g. tilde, umlaut, accent, etc.) . Sorry RoW!

The most universal RTF format seems to want 8-bit text encoding with code page escape (two hexadecimal digits following a backslash). So n with tilde (ñ) would be \'f1.

The only solution that occurs to me is to convert to NSUTF8StringEncoding and then use stringByReplacingOccurrencesOfString, but there are a lot characters and it seems tedious to have to replace every one of them manually. Is there a more efficient way that is escaping me? (pun intended) :)

Thanks for any suggestions.

+1  A: 

Check the value of characterAtIndex: if it is > 127, it is not ASCII, so escape the character.

Something like the following

- (NSString *)stringFormattedRTF:(NSString *)inputString
{
    NSMutableString *result = [NSMutableString string];

    for ( int index = 0; index < [inputString length]; index++ ) {
        NSString *temp = [inputString substringWithRange:NSMakeRange( index, 1 )];
        unichar tempchar = [inputString characterAtIndex:index];

        if ( tempchar > 127) {
            [result appendFormat:@"\\\'%02x", tempchar]; 
        } else {
            [result appendString:temp];
        }
    }
    return result;
}
falconcreek
This is not working as expected. Will update when a working solution is found
falconcreek
Thanks for the proposed answer and follow-up.
DenVog
Fixed method to return correct result
falconcreek
That got it. Thanks very much falconcreek!
DenVog
The above is working great for "special characters" like umlauts and accents. Any for suggestions on how to handle two-byte characters, such as Japanese and Chinese? It seems to me that the above should already be escaping those, but right now those characters are being converted to ????. Thanks.
DenVog