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;
}
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
\f0is important, that selects the charset. In this case character set 238, the charset for Eastern European languages. Note how the RTF contains the\fonttblcommand 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
\fonttblcommands 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.