// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Globalization; using System.Text; using System.Text.RegularExpressions; namespace E { public class Program { private static byte[] QPDecode(string s) { byte[] b = new byte[3*s.Length]; int six = 0; int bix = 0; while(six < s.Length) { if(s[six] == '=') { b[bix] = (byte)int.Parse(s.Substring(six + 1, 2), NumberStyles.HexNumber); six += 3; bix++; } else { b[bix] = (byte)s[six]; six++; bix++; } } Array.Resize(ref b, bix); return b; } private static readonly Regex re = new Regex(@"(=?)([A-Za-z0-9-]+)(\?[Qq]\?)([A-Za-z0-9=_.]*)(\?=)", RegexOptions.Compiled); public static string MailHeaderDecode(string s) { Match m = re.Match(s); if(m.Success) { return Encoding.GetEncoding(m.Groups[2].Value).GetString(QPDecode(m.Groups[4].Value)); } else { return s; } } public static void Main(string[] args) { string s = "=?iso-8859-1?Q?S=F8ren_s._efternavn?= "; Console.WriteLine(s); string s2 = MailHeaderDecode(s); Console.WriteLine(s2); } } }