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!

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.