2012-01-09 64 views
1

Iam無法解碼日文信息,最終輸出似乎是垃圾值。解碼引用的可打印信息

編碼ISO-2022-JP引用Printable消息:

「= 82 = B1 = 82 = EA = 82 = CD = 92P = 82 = C8 = 82 = E9 = 83E = 83X =83克= 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 「

代碼,用於解碼引用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()); 
    } 

最終輸出

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

任何想法來解決它?

+0

您的編碼..您使用Encoding.GetEncoding( 「ISO-2022-JP」) – MethodMan 2012-01-09 22:13:37

回答

0

試試這個

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

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

優秀的兄弟它的工作.. – user845405 2012-01-10 20:50:03

0

會有類似於這項工作..?剛剛的Sudu編碼這種快速

Encoding encoding = Encoding.GetEncoding("iso-2022-jp"); 
byte[] bytes = encoding.GetBytes(output); 
string uuEncoded = Convert.ToBase64String(bytes); 
+0

不,它不工作。它會引發編譯錯誤.Encoding。GetBygtes有無效的參數。 – user845405 2012-01-09 23:19:53

1

您可以在下面的文章展示了一招:

所以,你可以是這樣的:

private static string Decode(string input, string bodycharset) 
{ 
    Attachment attachment = Attachment.CreateAttachmentFromString("", "=?"+bodycharset+"?"+input+"?="); 
    return (attachment.Name); 
} 

您還需要添加:

using System.Net.Mail; 

我希望這有助於:-)

+1

當我使用你提供的解碼函數時,它返回給我這個字符串=?iso-2022-jp?= 8D = C4 = 82 = D1 = 83C = 83 = 93 = 83o = 83E = 83 = 93 = 83h = 83 = 81 = 81 [= 83 = 8B = 82 = CC = 83e = 83X = 83g = 8D = C4 = 82 = D1 = 83C = 83 = 93 = 83o = 83E = 83 = 93 = 83h = 83 = 81 = 81 [= 83 = 8B = 82 = CC = 83e = 83X = 83g = 8D = C4 = 82 = D1 = 83C = 83 = 93 = 83o = 83E = 83 = 93 = 83h = 83 = 81 = 81 [= 83 = 8B = 82 = CC = 83e = 83X = 83g = 8D = C4 = 82 = D1 = 83C = 83 = 93 = 83o = 83E = 83 = 93 = 83h = 83 = 81 = 81 = 8B = 82 = CC = 83e = 83X = 83g = 8D = C4 = 82 = D1 = 83C = 83 = 93 = 83o = 83E = 83 = 93 = 83h = 83 = 81 = 81 = CC = 83e = 83X = 83g – user845405 2012-01-09 22:53:32

+0

任何想法如何解決它?.. – user845405 2012-01-10 04:31:37

+0

對不起,我自己試了一下,它顯示我同樣的事情,然後我改變它:Attachment.CreateAttachmentFromString(「」,「=?」+ input +「 ?B +「bodycharset +」?=「,Encoding.GetEncoding(input),null);我得到了一些東西,但我不確定它是否正確? – Qorbani 2012-01-10 04:33:56

0

此引用Printable救了我的命。它像一個魅力工作!

public static byte[] FromHex(byte[] hexData) 
    { 
     if (hexData == null) 
     { 
      throw new ArgumentNullException("hexData"); 
     } 

     if (hexData.Length < 2 || (hexData.Length/(double)2 != Math.Floor(hexData.Length/(double)2))) 
     { 
      throw new Exception("Illegal hex data, hex data must be in two bytes pairs, for example: 0F,FF,A3,... ."); 
     } 

     MemoryStream retVal = new MemoryStream(hexData.Length/2); 
     // Loop hex value pairs 
     for (int i = 0; i < hexData.Length; i += 2) 
     { 
      byte[] hexPairInDecimal = new byte[2]; 
      // We need to convert hex char to decimal number, for example F = 15 
      for (int h = 0; h < 2; h++) 
      { 
       if (((char)hexData[i + h]) == '0') 
       { 
        hexPairInDecimal[h] = 0; 
       } 
       else if (((char)hexData[i + h]) == '1') 
       { 
        hexPairInDecimal[h] = 1; 
       } 
       else if (((char)hexData[i + h]) == '2') 
       { 
        hexPairInDecimal[h] = 2; 
       } 
       else if (((char)hexData[i + h]) == '3') 
       { 
        hexPairInDecimal[h] = 3; 
       } 
       else if (((char)hexData[i + h]) == '4') 
       { 
        hexPairInDecimal[h] = 4; 
       } 
       else if (((char)hexData[i + h]) == '5') 
       { 
        hexPairInDecimal[h] = 5; 
       } 
       else if (((char)hexData[i + h]) == '6') 
       { 
        hexPairInDecimal[h] = 6; 
       } 
       else if (((char)hexData[i + h]) == '7') 
       { 
        hexPairInDecimal[h] = 7; 
       } 
       else if (((char)hexData[i + h]) == '8') 
       { 
        hexPairInDecimal[h] = 8; 
       } 
       else if (((char)hexData[i + h]) == '9') 
       { 
        hexPairInDecimal[h] = 9; 
       } 
       else if (((char)hexData[i + h]) == 'A' || ((char)hexData[i + h]) == 'a') 
       { 
        hexPairInDecimal[h] = 10; 
       } 
       else if (((char)hexData[i + h]) == 'B' || ((char)hexData[i + h]) == 'b') 
       { 
        hexPairInDecimal[h] = 11; 
       } 
       else if (((char)hexData[i + h]) == 'C' || ((char)hexData[i + h]) == 'c') 
       { 
        hexPairInDecimal[h] = 12; 
       } 
       else if (((char)hexData[i + h]) == 'D' || ((char)hexData[i + h]) == 'd') 
       { 
        hexPairInDecimal[h] = 13; 
       } 
       else if (((char)hexData[i + h]) == 'E' || ((char)hexData[i + h]) == 'e') 
       { 
        hexPairInDecimal[h] = 14; 
       } 
       else if (((char)hexData[i + h]) == 'F' || ((char)hexData[i + h]) == 'f') 
       { 
        hexPairInDecimal[h] = 15; 
       } 
      } 

      // Join hex 4 bit(left hex cahr) + 4bit(right hex char) in bytes 8 it 
      retVal.WriteByte((byte)((hexPairInDecimal[0] << 4) | hexPairInDecimal[1])); 
     } 

     return retVal.ToArray(); 
    } 
    public static byte[] QuotedPrintableDecode(byte[] data) 
    { 
     if (data == null) 
     { 
      throw new ArgumentNullException("data"); 
     } 

     MemoryStream msRetVal = new MemoryStream(); 
     MemoryStream msSourceStream = new MemoryStream(data); 

     int b = msSourceStream.ReadByte(); 
     while (b > -1) 
     { 
      // Encoded 8-bit byte(=XX) or soft line break(=CRLF) 
      if (b == '=') 
      { 
       byte[] buffer = new byte[2]; 
       int nCount = msSourceStream.Read(buffer, 0, 2); 
       if (nCount == 2) 
       { 
        // Soft line break, line splitted, just skip CRLF 
        if (buffer[0] == '\r' && buffer[1] == '\n') 
        { 
        } 
        // This must be encoded 8-bit byte 
        else 
        { 
         try 
         { 
          msRetVal.Write(FromHex(buffer), 0, 1); 
         } 
         catch 
         { 
          // Illegal value after =, just leave it as is 
          msRetVal.WriteByte((byte)'='); 
          msRetVal.Write(buffer, 0, 2); 
         } 
        } 
       } 
       // Illegal =, just leave as it is 
       else 
       { 
        msRetVal.Write(buffer, 0, nCount); 
       } 
      } 
      // Just write back all other bytes 
      else 
      { 
       msRetVal.WriteByte((byte)b); 
      } 

      // Read next byte 
      b = msSourceStream.ReadByte(); 
     } 

     return msRetVal.ToArray(); 
    } 
+1

更好的是有一個字符串的輸入和輸出,因爲似乎有混亂和轉折的情況下從字符串轉換爲字節數組,或反之亦然。 – 2012-09-26 19:50:01

0

晚會晚了,但因爲我有一個解決這個惱人的任務。

我發現下面的鏈接解決方案:http://sourceforge.net/apps/trac/syncmldotnet/wiki/Quoted%20Printable

如果你只需要QP的從上面的鏈接進行解碼,拉你的代碼中,這三項功能:

HexDecoderEvaluator(Match m) 
    HexDecoder(string line) 
    Decode(string encodedText) 

而且然後只是:

var humanReadable = Decode(myQPString); 

無論如何,鏈接提供編碼的功能。

享受