' 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) 
Imports System
Imports System.Globalization
Imports System.Text
Imports System.Text.RegularExpressions

Namespace E
    Public Class Program
        Private Shared Function QPDecode(s As String) As Byte()
            Dim b As Byte() = New Byte(3 * s.Length){}
            Dim six As Integer = 0
            Dim bix As Integer = 0
            While six < s.Length
                If s(six) = "="C Then
                    b(bix) = CByte(Integer.Parse(s.Substring(six + 1, 2), NumberStyles.HexNumber))
                    six += 3
                    bix += 1
                Else
                    b(bix) = AscW(s(six))
                    six += 1
                    bix += 1
                End If
            End While
            Array.Resize(b, bix)
            Return b
        End Function
        Private Shared ReadOnly re As New Regex("(=?)([A-Za-z0-9-]+)(\?[Qq]\?)([A-Za-z0-9=_.]*)(\?=)", RegexOptions.Compiled)
        Public Shared Function MailHeaderDecode(s As String) As String
            Dim m As Match = re.Match(s)
            If m.Success Then
                Return Encoding.GetEncoding(m.Groups(2).Value).GetString(QPDecode(m.Groups(4).Value))
            Else
                Return s
            End If
        End Function
        Public Shared Sub Main(args As String())
            Dim s As String = "=?iso-8859-1?Q?S=F8ren_s._efternavn?= <sfgsf@sfgsfg.dk>"
            Console.WriteLine(s)
            Dim s2 As String = MailHeaderDecode(s)
            Console.WriteLine(s2)
        End Sub
    End Class
End Namespace
