Mends.One

ő becomes õ after RTF conversion in C#. Why?

C#, Rtf, Character Encoding

I have a method that converts strings to RTF-Strings.

For that i use the RichTextBox wich is provided by .NET the way it is described here:
How to convert a string to RTF in C#?

When I enter ő it returns {\rtf1 {'f5\f1}}. But that seems to be õ because I get that symbol, when I put it into a .rtf-file.

Why does that happen? And what can i do to solve this issue?

EDIT:

Here is the whole Method as i use it:

private static string ConvertToRtf(string text) {
        System.Windows.Forms.RichTextBox richTextBox = new System.Windows.Forms.RichTextBox();
        richTextBox.Text = text;
        int offset = richTextBox.Rtf.IndexOf(@"\f0\fs17") + 8;
        int length = richTextBox.Rtf.LastIndexOf(@"\par") - offset;
        string result = richTextBox.Rtf.Substring(offset, length).Substring(1);
        return result;
    }
0
T
Torben L.
Jump to: Answer 1

Answers (1)

The whole RTF string of the rtfBox looks like this (etc..)

That's fine and displays correctly. Your code snippet however doesn't make sense. You cannot just take a sliver of RTF and hope it displays properly. Particularly the \f0 is important, that selects the charset. In this case character set 238, the charset for Eastern European languages. Note how the RTF contains the \fonttbl command to assign f0.

So if you copy that sliver of RTF and use it elsewhere, like in some other RTB that was not initialized with the same \fonttbl commands then you'll get a character from the wrong charset. Like charset 0, which indeed displays õ instead.

Well, now you know why Unicode was invented ;)

The workaround is to only copy text from the RichTextBox.Text property. That's Unicode.

0
H
Hans Passant

Comments:

Torben L. said:
Ok, that fixed it. I will test if this fits for our circumstances. Thanks :)

Related Questions