How to output unicode string to RTF (using C#)

前端 未结 4 1619
小蘑菇
小蘑菇 2020-11-29 07:15

I\'m trying to output unicode string into RTF format. (using c# and winforms)

From wikipedia:

If a Unicode escape is required, the control wor

4条回答
  •  粉色の甜心
    2020-11-29 08:06

    Fixed code from accepted answer - added special character escaping, as described in this link

    static string GetRtfUnicodeEscapedString(string s)
    {
        var sb = new StringBuilder();
        foreach (var c in s)
        {
            if(c == '\\' || c == '{' || c == '}')
                sb.Append(@"\" + c);
            else if (c <= 0x7f)
                sb.Append(c);
            else
                sb.Append("\\u" + Convert.ToUInt32(c) + "?");
        }
        return sb.ToString();
    }
    

提交回复
热议问题