Convert strings to the RTF format-the RTF Research Series

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.