Encode/Decode Quoted Printable

By | 2019-12-31

Very fast function to encode or decode Quoted-Printable.

VB6 only, but you can make it work with other versions, with a 3rd party replace function.

Public Function DecodeQP(ByRef StrToDecode As String) As String

   Dim sTemp As String
   Dim i As Integer
   
   sTemp = StrToDecode
   For i = 255 To 127 Step -1
      If InStr(1, sTemp, "=" & Hex$(i)) <> 0 Then sTemp = Replace(sTemp, "=" & Hex$(i), Chr$(i))
   Next
   If InStr(1, sTemp, "=" & Hex$(61)) <> 0 Then sTemp = Replace(sTemp, "=" & Hex$(61), Chr$(255) & Chr$(254))
   For i = 32 To 10 Step -1
      If InStr(1, sTemp, "=" & Hex$(i)) <> 0 Then sTemp = Replace(sTemp, "=" & Hex$(i), Chr$(i))
   Next
   For i = 9 To 0 Step -1
      If InStr(1, sTemp, "=" & "0" & Hex$(i)) <> 0 Then sTemp = Replace(sTemp, "=" & Hex$(i), Chr$(i))
   Next
   
   sTemp = Replace(sTemp, "=", "")
   sTemp = Replace(sTemp, Chr$(255) & Chr$(254), "=")
   DecodeQP = sTemp

End Function

Public Function EncodeQP(ByRef StrToEncode As String) As String

   Dim sTemp As String
   Dim i As Integer
   
   sTemp = StrToEncode
   For i = 255 To 127 Step -1
      If InStr(1, sTemp, Chr$(i)) <> 0 Then sTemp = Replace(sTemp, Chr$(i), "=" & Hex$(i))
   Next
   If InStr(1, sTemp, Chr$(61)) <> 0 Then sTemp = Replace(sTemp, Chr$(61), "=" & Hex$(61))
   For i = 32 To 10 Step -1
      If InStr(1, sTemp, Chr$(i)) <> 0 Then sTemp = Replace(sTemp, Chr$(i), "=" & Hex$(i))
   Next
   For i = 9 To 0 Step -1
      If InStr(1, sTemp, Chr$(i)) <> 0 Then sTemp = Replace(sTemp, Chr$(i), "=" & "0" & Hex$(i))
   Next
   
   EncodeQP = sTemp

End Function

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.