Search code examples
c#asp.net.netdecodingquoted-printable

Decoding Quoted printable message


Iam having trouble decoding Japanese message, the final output seems to be a garbage value.

Encoded ISO-2022-JP Quoted Printable Message:

"=82=B1=82=EA=82=CD=92P=82=C8=82=E9=83e=83X=83g=82=C5=82=B7=82=DD=82=C8=82=B3=\r\n=82=F1=81A=82=B1 =82=F1=82=C9=82=BF=82=CD"

Code for decoding Quoted Printable:

 private static string Decode(string input, string bodycharset)
    {
        var i = 0;
        var output = new List<byte>();
        while (i < input.Length)
        {
            if (input[i] == '=' && input[i + 1] == '\r' && input[i + 2] == '\n')
            {
                //Skip
                i += 3;
            }
            else if (input[i] == '=')
            {
                string sHex = input;
                sHex = sHex.Substring(i + 1, 2);
                int hex = Convert.ToInt32(sHex, 16);
                byte b = Convert.ToByte(hex);
                output.Add(b);
                i += 3;
            }
            else
            {
                output.Add((byte)input[i]);
                i++;
            }
        }
        if (String.IsNullOrEmpty(bodycharset))
            return Encoding.UTF8.GetString(output.ToArray());
        else
            return Encoding.GetEncoding(bodycharset).GetString(output.ToArray());
    }

Final Output:

・ア・・・ヘ・P・ネ・・・e・X・g・ナ・キ・ン・ネ・ウ・・・A・ア・・・ノ・ソ・ヘ

Any ideas to resolve it?.


Solution

  • try this

    var str = Decode(inp, "Shift_JIS");
    

    or

    var str = Decode(inp, "sjis");