As a result of the project, I have made in-depth research on the RTF format. Some of my experiences are shared with the source code!
Let's take a look at the source code!
Platform and language: vs2005, C #
/// <Summary>
/// Convert string to the RTF type
/// </Summary>
/// <Param name = "STRs"> </param>
/// <Returns> </returns>
Public static string str2rtf (string STRs)
{
String tmpstr = "", tmpstr2 = "", strtortf = "";
Int lstrlen = STRs. length;
If (lstrlen = 0)
Return "";
Foreach (char C in STRs)
{
Tmpstr = C. tostring ();
Int tmpasc = (INT) C;
If (tmpasc> 126) // convert the text in non-ASCII range to the RTF Format
{
Tmpstr = charto16 (C );
If (tmpstr. Length = 1)
Tmpstr = @ "\ '0" + tmpstr; // convert a special control symbol whose HEX value is less than two digits
Else if (tmpstr. Length = 2)
Tmpstr = @ "\ '" + tmpstr; // converts a special symbol whose HEX value is equal to two digits.
Else
{
Tmpstr2 = tmpstr. substring (tmpstr. Length-2, 2); // right (tmpstr, 2 );
Tmpstr = tmpstr. substring (0, 2); // left (tmpstr, 2 );
Tmpstr = @ "\ '" + tmpstr + @ "\'" + tmpstr2; // convert the hex value to a four-character non-English character internal code
}
}
// If (tmpstr = "\ '0d" | tmpstr = "\ '0a") // then' performs Special Control on line feed characters: 0d 0a
// Tmpstr = @ "}{\ insrsid198535 \ par }{\ insrsid198535 \ Loch \ af0 \ hich \ af0 \ dbch \ f13 ";
Strtortf + = tmpstr;
}
Return strtortf;
}
/// <Summary>
/// Convert Char to hexadecimal character
/// </Summary>
/// <Param name = "ch"> </param>
/// <Returns> </returns>
Public static string charto16 (char ch)
{
System. Text. Encoding CHS = system. Text. encoding. getencoding ("gb2312 ");
Byte [] bytes = CHS. getbytes (ch. tostring ());
String STR = "";
For (INT I = 0; I <bytes. length; I ++)
{
STR + = string. Format ("{0: X2}", bytes [I]);
}
Return Str. tolower ();
}
I will release it later!