VB.NET 将 Unicode 8 (UTF8) 转换为常规美国 ASCII

发布于 2024-12-25 06:00:04 字数 4159 浏览 2 评论 0原文

我这里有问题是调试输出

"?ufn74tn5187r&key=6e6e0936c4e6c48be56a72eba8964df0"

应该是

"?u=83n74tn5187r&key=6e6e0936c4e6c48be56a72eba8964df0"

我尝试过另一个类似问题的解决方案,但失败了。

Dim uni As Byte() = Encoding.GetEncoding(437).GetBytes("?uƒn74tn5187r&key=6e6e0936c4e6c48be56a72eba8964df0")
Dim Ascii As String = Encoding.ASCII.GetString(uni)

ASCII = "?u?n74tn5187r&key=6e6e0936c4e6c48be56a72eba8964df0"

我猜我必须猜测 437.. 也许对所有数字进行暴力攻击,直到匹配 ?u=83 code> 来自 ?uf

真的,我正在尝试阅读Unicode-32(来自电子邮件的巴西格式文本 (POP3)。现在我想一下,使用此函数可能会弄乱 =83

但是如果没有此函数,POP3 电子邮件的正文将包含也许像 urlencode() 的变体一样无用,但是..它使用 =20 而不是%20

知道如何解决这个问题。

 Public Shared Function DecodeQuotedPrintable(ByVal Message As String, Optional ByVal QuickClean As Boolean = False) As String
        'set up StringBuilder object with data stripped of any line continuation tags
        Dim Msg As New StringBuilder(Message.Replace("=" & vbCrLf, vbNullString))

        If QuickClean Then                                                  'perform a quick clean (clean up common basics)
            Return Msg.Replace("=" & vbCrLf, vbNullString).Replace("=0D", vbCr).Replace("=0A", _
                                   vbLf).Replace("=20", " ").Replace("=3D", "=").ToString
        Else                                                                'perform total cleaning
            'store 2-character hex values that require a leading "0"
            Dim HxData As String = "X0102030405060708090A0B0C0D0E0F"
            For Idx As Integer = 1 To &HF                                   'initially process codes 1-15, which require a leading zero
                Msg.Replace("=" & Mid(HxData, Idx << 1, 2), Chr(Idx))       'replace hex data with single character code (SHIFT is faster)
            Next
            For idx As Integer = &H10 To &HFF                               'process the whole 8-bit extended ASCII gambit
                Msg.Replace("=" & Hex(idx), Chr(idx))                       'replace hex data with single character code
            Next
            Return Msg.ToString                                             'return result string
        End If
    End Function

我想 我尝试修复该功能(如果它真的导致问题?我永远不会知道)

Public Shared Function DecodeQuotedPrintable(ByVal Message As String, Optional ByVal QuickClean As Boolean = False) As String
    'set up StringBuilder object with data stripped of any line continuation tags
    Dim Msg As New StringBuilder(Message.Replace("=" & vbCrLf, vbNullString))

    If QuickClean Then                                                  'perform a quick clean (clean up common basics)
        Return Msg.Replace("=" & vbCrLf, vbNullString).Replace("=0D", vbCr).Replace("=0A",
                           vbLf).Replace("=20", " ").Replace("=3D", "=").ToString
    Else                                                                'perform total cleaning
        'store 2-character hex values that require a leading "0"

        Msg.Replace("=" & vbCrLf, vbNullString).Replace("=0D", vbCr).Replace("=0A",
                           vbLf).Replace("=20", " ").Replace("=3D", "%$#@[EQUALS]@#$%").ToString()

        Dim HxData As String = "X0102030405060708090A0B0C0D0E0F"
        For Idx As Integer = 1 To &HF                                   'initially process codes 1-15, which require a leading zero
            Msg.Replace("=" & Mid(HxData, Idx << 1, 2), Chr(Idx))       'replace hex data with single character code (SHIFT is faster)
        Next
        For idx As Integer = &H10 To &HFF                               'process the whole 8-bit extended ASCII gambit
            Msg.Replace("=" & Hex(idx), Chr(idx))                       'replace hex data with single character code
        Next

        Msg.Replace("%$#@[EQUALS]@#$%", "=")

        Return Msg.ToString                                             'return result string
    End If
End Function

I have thing problem here is the debugging outputs

"?uƒn74tn5187r&key=6e6e0936c4e6c48be56a72eba8964df0"

should be

"?u=83n74tn5187r&key=6e6e0936c4e6c48be56a72eba8964df0"

I have tried solution from another similar question and it failed me.

Dim uni As Byte() = Encoding.GetEncoding(437).GetBytes("?uƒn74tn5187r&key=6e6e0936c4e6c48be56a72eba8964df0")
Dim Ascii As String = Encoding.ASCII.GetString(uni)

Ascii =
"?u?n74tn5187r&key=6e6e0936c4e6c48be56a72eba8964df0"

I'm guessing I have to guess the 437.. maybe a brute force attack on all numbers until the match of ?u=83 from ?uƒ

Really I am trying to read a Unicode-32 (Brasil formatted text from email (POP3). Now that I think about it =83 could be messed up using this function here.

But without this function, the body of the POP3 email will contain maybe useless like variant of urlencode() but.. instead of %20 it uses =20.

I wonder how to fix this.

 Public Shared Function DecodeQuotedPrintable(ByVal Message As String, Optional ByVal QuickClean As Boolean = False) As String
        'set up StringBuilder object with data stripped of any line continuation tags
        Dim Msg As New StringBuilder(Message.Replace("=" & vbCrLf, vbNullString))

        If QuickClean Then                                                  'perform a quick clean (clean up common basics)
            Return Msg.Replace("=" & vbCrLf, vbNullString).Replace("=0D", vbCr).Replace("=0A", _
                                   vbLf).Replace("=20", " ").Replace("=3D", "=").ToString
        Else                                                                'perform total cleaning
            'store 2-character hex values that require a leading "0"
            Dim HxData As String = "X0102030405060708090A0B0C0D0E0F"
            For Idx As Integer = 1 To &HF                                   'initially process codes 1-15, which require a leading zero
                Msg.Replace("=" & Mid(HxData, Idx << 1, 2), Chr(Idx))       'replace hex data with single character code (SHIFT is faster)
            Next
            For idx As Integer = &H10 To &HFF                               'process the whole 8-bit extended ASCII gambit
                Msg.Replace("=" & Hex(idx), Chr(idx))                       'replace hex data with single character code
            Next
            Return Msg.ToString                                             'return result string
        End If
    End Function

Edit:
My attempt at fixing the function (if it really causes the problem? I'll never know)

Public Shared Function DecodeQuotedPrintable(ByVal Message As String, Optional ByVal QuickClean As Boolean = False) As String
    'set up StringBuilder object with data stripped of any line continuation tags
    Dim Msg As New StringBuilder(Message.Replace("=" & vbCrLf, vbNullString))

    If QuickClean Then                                                  'perform a quick clean (clean up common basics)
        Return Msg.Replace("=" & vbCrLf, vbNullString).Replace("=0D", vbCr).Replace("=0A",
                           vbLf).Replace("=20", " ").Replace("=3D", "=").ToString
    Else                                                                'perform total cleaning
        'store 2-character hex values that require a leading "0"

        Msg.Replace("=" & vbCrLf, vbNullString).Replace("=0D", vbCr).Replace("=0A",
                           vbLf).Replace("=20", " ").Replace("=3D", "%$#@[EQUALS]@#$%").ToString()

        Dim HxData As String = "X0102030405060708090A0B0C0D0E0F"
        For Idx As Integer = 1 To &HF                                   'initially process codes 1-15, which require a leading zero
            Msg.Replace("=" & Mid(HxData, Idx << 1, 2), Chr(Idx))       'replace hex data with single character code (SHIFT is faster)
        Next
        For idx As Integer = &H10 To &HFF                               'process the whole 8-bit extended ASCII gambit
            Msg.Replace("=" & Hex(idx), Chr(idx))                       'replace hex data with single character code
        Next

        Msg.Replace("%$#@[EQUALS]@#$%", "=")

        Return Msg.ToString                                             'return result string
    End If
End Function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

守护在此方 2025-01-01 06:00:04

“f”在 Windows-1252 字符集中的 Quoted Printable 编码中由 =83 表示。

"ƒ" is represented by =83 in Quoted Printable encoding in the Windows-1252 character set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文