How to convert Quoted-Print String

后端 未结 5 388
执笔经年
执笔经年 2021-01-14 22:18

I\'m working on French String in .NET Decoding a Mail body , I receive \"Chasn=C3=A9 sur illet\" I would like to get \"Chasné sur illet\" and i don\'t find any solution ave

5条回答
  •  一向
    一向 (楼主)
    2021-01-14 22:28

    We had an issue with this method - that it is VERY slow. The following enhanced performance A LOT

    public static string FromMailTransferEncoding(this string messageText, Encoding enc, string transferEncoding)
    {
        if (string.IsNullOrEmpty(transferEncoding)) 
            return messageText;
    
        if ("quoted-printable".Equals(transferEncoding.ToLower())) 
        {
            StringBuilder sb = new StringBuilder();               
            string delimitorRegEx = @"=[\r][\n]";
            string[] parts = Regex.Split(messageText, delimitorRegEx);
    
            foreach (string part in parts)
            {
                string subPart = part;
                Regex occurences = new Regex(@"(=[0-9A-Z][0-9A-Z])+", RegexOptions.Multiline);
                MatchCollection matches = occurences.Matches(subPart);
    
                foreach (Match m in matches)
                {
                    byte[] bytes = new byte[m.Value.Length / 3];
                    for (int i = 0; i < bytes.Length; i++)
                    {
                        string hex = m.Value.Substring(i * 3 + 1, 2);
                        int iHex = Convert.ToInt32(hex, 16);
                        bytes[i] = Convert.ToByte(iHex);
                    }
    
                    subPart = occurences.Replace(subPart, enc.GetString(bytes), 1);
                }
    
                sb.Append(subPart);
            }
            return sb.ToString();
        }        
    return messageText;
    }
    

提交回复
热议问题