none
Decode QuotedPrintable using C# RRS feed

  • Question

  • 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?.


    Ram
    Monday, January 9, 2012 10:14 PM

Answers

  • Problem solved by using "Shift_JIS" charset instead of "ISO-2022-JP" for decoding.

     

    Modified and working code: 

            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
                {
                    if (String.Compare(bodycharset, "ISO-2022-JP", true) == 0)
                        return Encoding.GetEncoding("Shift_JIS").GetString(output.ToArray());
                    else
                        return Encoding.GetEncoding(bodycharset).GetString(output.ToArray());
                }      
       
            }
    

     Thank you guys!..

     


    Ram
    • Marked as answer by ssh123 Wednesday, January 11, 2012 12:46 PM
    Wednesday, January 11, 2012 12:45 PM

All replies

  • Any One ?...
    Ram
    Tuesday, January 10, 2012 2:31 AM
  • Can you please be more specific?
    Tuesday, January 10, 2012 3:11 PM
    • Marked as answer by ssh123 Wednesday, January 11, 2012 12:45 PM
    • Unmarked as answer by ssh123 Wednesday, January 11, 2012 12:45 PM
    • Marked as answer by ssh123 Wednesday, January 11, 2012 12:45 PM
    • Unmarked as answer by ssh123 Wednesday, January 11, 2012 12:45 PM
    Wednesday, January 11, 2012 3:44 AM
    • Marked as answer by ssh123 Wednesday, January 11, 2012 12:45 PM
    • Unmarked as answer by ssh123 Wednesday, January 11, 2012 12:46 PM
    Wednesday, January 11, 2012 3:55 AM
  • Problem solved by using "Shift_JIS" charset instead of "ISO-2022-JP" for decoding.

     

    Modified and working code: 

            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
                {
                    if (String.Compare(bodycharset, "ISO-2022-JP", true) == 0)
                        return Encoding.GetEncoding("Shift_JIS").GetString(output.ToArray());
                    else
                        return Encoding.GetEncoding(bodycharset).GetString(output.ToArray());
                }      
       
            }
    

     Thank you guys!..

     


    Ram
    • Marked as answer by ssh123 Wednesday, January 11, 2012 12:46 PM
    Wednesday, January 11, 2012 12:45 PM
  • Glad to hearing you got it works, Thanks for sharing your solution here.

     


    Best Regards,
    Rocky Yue[MSFT]
    MSDN Community Support | Feedback to us
    Wednesday, January 18, 2012 3:17 AM
    Moderator