UTF8 Sting Convertieren

  • Hallo,

    Ich habe einen String der wie folgt aussieht:

    =?utf-8?b?QmFja3VwIEV4ZWMtTWVsZHVuZzogQXVmdHJhZyBlcmZvbGdyZWljaA==?=

    dies ist ein Teil einer Betreffzeiele.
    Ich möchte nun diese Zeile im Klarzext darstellen, nur leider habe ich bis jetzt überhaupt keinen Ansatz.

    Ich Habe schon einiges Ausprobiert.

    und auch das hier gefunden:

    Spoiler anzeigen
    [autoit]

    Func Asc2Unicode($AscString, $addBOM = false)
    Local $BufferSize = StringLen($AscString) * 2
    Local $FullUniStr = DllStructCreate("byte[" & $BufferSize + 2 & "]")
    Local $Buffer = DllStructCreate("byte[" & $BufferSize & "]", DllStructGetPtr($FullUniStr) + 2)
    Local $Return = DllCall("Kernel32.dll", "int", "MultiByteToWideChar", _
    "int", 0, _
    "int", 0, _
    "str", $AscString, _
    "int", StringLen($AscString), _
    "ptr", DllStructGetPtr($Buffer, 1), _
    "int", $BufferSize)
    DllStructSetData($FullUniStr, 1, 0xFF, 1)
    DllStructSetData($FullUniStr, 1, 0xFE, 2)
    If $addBOM then
    Return DllStructGetData($FullUniStr, 1)
    Else
    Return DllStructGetData($Buffer, 1)
    Endif
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func Unicode2Asc($UniString)
    If Not isBinary($UniString) Then
    SetError(1)
    Return $UniString
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    Local $BufferLen = StringLen($UniString)
    Local $Input = DllStructCreate("byte[" & $BufferLen & "]")
    Local $Output = DllStructCreate("char[" & $BufferLen & "]")
    DllStructSetData($Input, 1, $UniString)
    Local $Return = DllCall("kernel32.dll", "int", "WideCharToMultiByte", _
    "int", 0, _
    "int", 0, _
    "ptr", DllStructGetPtr($Input), _
    "int", $BufferLen / 2, _
    "ptr", DllStructGetPtr($Output), _
    "int", $BufferLen, _
    "int", 0, _
    "int", 0)
    Local $AscString = DllStructGetData($Output, 1)
    $Output = 0
    $Input = 0
    Return $AscString
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func Unicode2Utf8($UniString)
    If Not IsBinary($UniString) Then
    SetError(1)
    Return $UniString
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    Local $UniStringLen = StringLen($UniString)
    Local $BufferLen = $UniStringLen * 2
    Local $Input = DllStructCreate("byte[" & $BufferLen & "]")
    Local $Output = DllStructCreate("char[" & $BufferLen & "]")
    DllStructSetData($Input, 1, $UniString)
    Local $Return = DllCall("kernel32.dll", "int", "WideCharToMultiByte", _
    "int", 65001, _
    "int", 0, _
    "ptr", DllStructGetPtr($Input), _
    "int", $UniStringLen / 2, _
    "ptr", DllStructGetPtr($Output), _
    "int", $BufferLen, _
    "int", 0, _
    "int", 0)
    Local $Utf8String = DllStructGetData($Output, 1)
    $Output = 0
    $Input = 0
    Return $Utf8String
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func Utf82Unicode($Utf8String)
    Local $BufferSize = StringLen($Utf8String) * 2
    Local $Buffer = DllStructCreate("byte[" & $BufferSize & "]")
    Local $Return = DllCall("Kernel32.dll", "int", "MultiByteToWideChar", _
    "int", 65001, _
    "int", 0, _
    "str", $Utf8String, _
    "int", StringLen($Utf8String), _
    "ptr", DllStructGetPtr($Buffer), _
    "int", $BufferSize)
    Local $UnicodeString = StringLeft(DllStructGetData($Buffer, 1), $Return[0] * 2)
    $Buffer = 0
    Return $UnicodeString
    EndFunc

    [/autoit]

    Aber das alles hilft mir nicht weiter.

    Dank für eure hilfe

  • Wenn es nur ein paar Zeichen sind , sollte die Funktion auch reichen (nicht sehr schnell):

    Spoiler anzeigen
    [autoit]

    Func Base64Decode($s)
    ; by Eddy
    Local $key = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=', _
    $t = '', $p = -8, $a = 0, $c, $d, $len = StringLen($s)
    For $i = 1 to $len
    $c = StringInStr($key, StringMid($s, $i, 1), 1) - 1
    If $c < 0 Then ContinueLoop
    $a = BitOR(BitShift($a, -6), BitAND($c, 63))
    $p = $p + 6
    If $p >= 0 Then
    $d = BitAND(BitShift($a, $p), 255)
    If $c <> 64 Then $t = $t & Chr($d)
    $a = BitAND($a, 63)
    $p = $p - 8
    EndIf
    Next
    Return $t
    EndFunc ;==>Base64Decode

    [/autoit]


    steht glaub ich irgendwo in diesem Thema.

    [EDIT]
    Oder das dort:
    http://www.autoitscript.com/forum/index.php?showtopic=81332

    Grüße
    Stilgar

    Einmal editiert, zuletzt von Stilgar (15. April 2009 um 14:46)

  • ich habe dir mal Code zum dekodieren geschrieben. Der funktioniert auch mit anderen Kodierungen als UTF-8, solange sie in der Registry gespeichert sind:

    Spoiler anzeigen
    [autoit]

    Func _GetCodepage($charset)
    ; Prog@ndy
    Local Const $PATH = "HKEY_CLASSES_ROOT\MIME\Database\Charset\"
    Local $alias
    While 1
    $alias = RegRead($PATH & $charset, "AliasForCharset")
    If @error Then ExitLoop
    $charset = $alias
    WEnd
    Local $result = RegRead($PATH & $charset, "InternetEncoding")
    Return SetError(@error,@extended,$result)
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _MultiByteToWideChar($CodePage,$dwFlags,$lpMultiByteStr,$cbMultiByte,$lpWideCharStr,$cchWideChar)
    ; Prog@ndy
    Local $TypeMBStr = "str"
    If IsPtr($lpMultiByteStr) Then $TypeMBStr = "ptr"
    Local $aResult = DllCall("Kernel32.dll", "int", "MultiByteToWideChar", "UINT", $CodePage, "DWORD", $dwFlags, _
    $TypeMBStr, $lpMultiByteStr, "int", $cbMultiByte, "ptr", $lpWideCharStr, "int", $cchWideChar)
    If @error Then Return SetError(@error,0,0)
    Return $aResult[0]
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _TranslateString($String, $Codepage)
    ; Prog@ndy
    SetError(0)
    If Not (IsInt($Codepage) And $Codepage>0) Then $Codepage = _GetCodepage($Codepage)
    If @error Or $Codepage=0 Then Return SetError(1,0,"")
    Local $Length = _MultiByteToWideChar($Codepage, 0, $String, StringLen($String), 0, 0)
    Local $Buffer = DllStructCreate("wchar[" & $Length+1 & "]")
    If Not _MultiByteToWideChar($Codepage, 0, $String, StringLen($String), DllStructGetPtr($Buffer), $Length) Then
    Return SetError(2,0,"")
    EndIf
    Return DllStructGetData($Buffer,1)
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func Base64Decode($s)
    ; by Eddy
    Local $key = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=', _
    $t = '', $p = -8, $a = 0, $c, $d, $len = StringLen($s)
    For $i = 1 to $len
    $c = StringInStr($key, StringMid($s, $i, 1), 1) - 1
    If $c < 0 Then ContinueLoop
    $a = BitOR(BitShift($a, -6), BitAND($c, 63))
    $p = $p + 6
    If $p >= 0 Then
    $d = BitAND(BitShift($a, $p), 255)
    If $c <> 64 Then $t = $t & Chr($d)
    $a = BitAND($a, 63)
    $p = $p - 8
    EndIf
    Next
    Return $t
    EndFunc ;==>Base64Decode

    [/autoit] [autoit][/autoit] [autoit]

    Func _QuotedPrintableDecode($s, $IsQ=False)
    ; Prog@ndy
    If $IsQ Then $s = StringReplace($s, "_", " ")
    $s = StringSplit($s,"=")
    Local $result = $s[1]
    For $i = 2 To $s[0]
    $result &= Chr(Dec(StringLeft($s[$i],2))) & StringTrimLeft($s[$i],2)
    Next
    Return $result
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _DecodeEncodedWord($theString)
    ; Prog@ndy
    Local $EncodedChunks = StringRegExp($theString, "=\?(.+?)\?(.)\?(.*?)\?=",4)
    If Not @error Then
    For $Index = 0 To UBound($EncodedChunks)-1
    $parts = $EncodedChunks[$Index]
    Switch $parts[2]
    Case "B", "b"
    $parts[3] = Base64Decode($parts[3])
    Case "Q", "q"
    $parts[3] = _QuotedPrintableDecode($parts[3],True)
    EndSwitch
    $parts[3] = _TranslateString($parts[3],$parts[1])
    $theString = StringReplace($theString,$parts[0],$parts[3])
    Next
    EndIf
    Return $theString
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    $teststring = "=?utf-8?b?QmFja3VwIEV4ZWMtTWVsZHVuZzogQXVmdHJhZyBlcmZvbGdyZWljaA==?="
    MsgBox(0, '', _DecodeEncodedWord($teststring))
    MsgBox(0, '', _DecodeEncodedWord("Subject: =?iso-8859-1?Q?=A1Hola,_se=F1or!?="))

    [/autoit]


    //Edit: QuotedPrintable-Unterstützung
    //Edit: Mehr Info zum Format: http://en.wikipedia.org/wiki/MIME#Encoded-Word
    //Edit: Mehrere kodierte Teile in einem String möglich.

    4 Mal editiert, zuletzt von progandy (15. April 2009 um 15:53)

  • Die kodiereung muss aber nicht immer UTF-8 sein. Das hängt vom sendenden Mail-Programm ab, wie es kodiert. Hast du dir mal meine Funktionen angeschaut? Dort hab ich das universell gelöst.