を変換するために、私が受け取る「Chasn = C3 = A9シュルillet」 と私は2日間のWeb検索で何の解決策も見つけられません。どのように私は、メール本文のデコード.NET にフランス語の文字列に取り組んでいる引用符で囲まれ、印刷文字列
C#ou VB.NET 誰でも助けてくれますか?
ありがとう
を変換するために、私が受け取る「Chasn = C3 = A9シュルillet」 と私は2日間のWeb検索で何の解決策も見つけられません。どのように私は、メール本文のデコード.NET にフランス語の文字列に取り組んでいる引用符で囲まれ、印刷文字列
C#ou VB.NET 誰でも助けてくれますか?
ありがとう
これはUTF8エンコードです。このポストを使用して
:
using System;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(DecodeQuotedPrintable("Chasn=C3=A9 sur illet"));
Console.ReadKey();
}
static string DecodeQuotedPrintable(string input)
{
var occurences = new Regex(@"(=[0-9A-Z][0-9A-Z])+", RegexOptions.Multiline);
var matches = occurences.Matches(input);
foreach (Match m in matches)
{
byte[] bytes = new byte[m.Value.Length/3];
for (int i = 0; i < bytes.Length; i++)
{
string hex = m.Value.Substring(i * 3 + 1, 2);
int iHex = Convert.ToInt32(hex, 16);
bytes[i] = Convert.ToByte(iHex);
}
input = input.Replace(m.Value, Encoding.UTF8.GetString(bytes));
}
return input.Replace("=rn", "");
}
}
}
static string ConverFromHex(string source)
{
string target = string.Empty;
int startPos = source.IndexOf('=', 0);
int prevStartPos = 0;
while (startPos >= 0)
{
// concat with substring from source
target += source.Substring(prevStartPos, startPos - prevStartPos);
// next offset
startPos++;
// update prev pos
prevStartPos = startPos;
// get substring
string hexString = source.Substring(startPos, 2);
// get int equiv
int hexNum = 0;
if (int.TryParse(hexString, System.Globalization.NumberStyles.AllowHexSpecifier, System.Globalization.CultureInfo.InvariantCulture, out hexNum))
{
// add to target string
target += (char)hexNum;
// add hex length
prevStartPos += 2;
}
// next occurence
startPos = source.IndexOf('=', startPos);
}
// add rest of source
target += source.Substring(prevStartPos);
return target;
}
このコードはOPの文字列を正しく処理しません。結果は "Chasnésur leillet"です。あなたのコードでは、各16進数を別の文字として扱いますが、 "= C3 = A9"は1文字 "é"を表すものとします。 – Blackwood
から:https://stackoverflow.com/a/36803911/6403521 私のソリューション
http://www.dpit.co.uk/decoding-quoted-printable-email-in-c/
ここでは、コードは(助けた場合の答えを受け入れることを忘れないでください)です:
[TestMethod]
public void TestMethod1()
{
Assert.AreEqual("La Bouichère", quotedprintable("La Bouich=C3=A8re", "utf-8"));
Assert.AreEqual("Chasné sur illet", quotedprintable("Chasn=C3=A9 sur illet", "utf-8"));
Assert.AreEqual("é è", quotedprintable("=C3=A9 =C3=A8", "utf-8"));
}
private string quotedprintable(string pStrIn, string encoding)
{
String strOut = pStrIn.Replace("=\r\n", "");
// Find the first =
int position = strOut.IndexOf("=");
while (position != -1)
{
// String before the =
string leftpart = strOut.Substring(0, position);
// get the QuotedPrintable String in a ArrayList
System.Collections.ArrayList hex = new System.Collections.ArrayList();
// The first Part
hex.Add(strOut.Substring(1 + position, 2));
// Look for the next parts
while (position + 3 < strOut.Length && strOut.Substring(position + 3, 1) == "=")
{
position = position + 3;
hex.Add(strOut.Substring(1 + position, 2));
}
// In the hex Array, we have two items
// Convert using the GetEncoding Function
byte[] bytes = new byte[hex.Count];
for (int i = 0; i < hex.Count; i++)
{
bytes[i] = System.Convert.ToByte(new string(((string)hex[i]).ToCharArray()), 16);
}
string equivalent = System.Text.Encoding.GetEncoding(encoding).GetString(bytes);
// Part of the orignal String after the last QP Symbol
string rightpart = strOut.Substring(position + 3);
// Re build the String
strOut = leftpart + equivalent + rightpart;
// find the new QP Position
position = leftpart.Length + equivalent.Length;
if (rightpart.Length == 0)
{
position = -1;
}
else
{
position = strOut.IndexOf("=", position + 1);
}
}
return strOut;
}
神様、これはあまりにも複雑です。私の答えを試しましたか? – ib11
またはすべての最も簡単な、ちょうど私MimeKitライブラリからQuotedPrintableDecoderを使用します。他の回答は、上記復号されたコンテンツは、ASCIIまたはUTF-8になると仮定し、それは必ずしもないことを
static string DecodeQuotedPrintable (string input, string charset)
{
var decoder = new QuotedPrintableDecoder();
var buffer = Encoding.ASCII.GetBytes (input);
var output = new byte[decoder.EstimateOutputLength (buffer.Length)];
int used = decoder.Decode (buffer, 0, buffer.Length, output);
var encoding = Encoding.GetEncoding (charset);
return encoding.GetString (output, 0, used);
}
注意場合。デコードしているMIMEパートのContent-Type
ヘッダーからcharset
パラメータを取得する必要があります。
もちろん、その情報を取得する方法がわからない場合は、単に私の素晴らしいMailKitライブラリを使用してIMAPからMIME部分を取得して、この作業をすべて行うことができます。
この方法に問題がありました。非常に遅いです。 次の拡張パフォーマンスA LOT
public static string FromMailTransferEncoding(this string messageText, Encoding enc, string transferEncoding)
{
if (string.IsNullOrEmpty(transferEncoding))
return messageText;
if ("quoted-printable".Equals(transferEncoding.ToLower()))
{
StringBuilder sb = new StringBuilder();
string delimitorRegEx = @"=[\r][\n]";
string[] parts = Regex.Split(messageText, delimitorRegEx);
foreach (string part in parts)
{
string subPart = part;
Regex occurences = new Regex(@"(=[0-9A-Z][0-9A-Z])+", RegexOptions.Multiline);
MatchCollection matches = occurences.Matches(subPart);
foreach (Match m in matches)
{
byte[] bytes = new byte[m.Value.Length/3];
for (int i = 0; i < bytes.Length; i++)
{
string hex = m.Value.Substring(i * 3 + 1, 2);
int iHex = Convert.ToInt32(hex, 16);
bytes[i] = Convert.ToByte(iHex);
}
subPart = occurences.Replace(subPart, enc.GetString(bytes), 1);
}
sb.Append(subPart);
}
return sb.ToString();
}
return messageText;
}
文字列を設定する場所にコードのビットを投稿できますか? –
こんにちは、私の文字列は、IMAPサーバーから来ています 私はメッセージを読んで、IMAP COMMAND:FETCH BODY [TEXT]メッセージボディ本文を取得し、それは私にQuoted_printable形式でコード化された文字列を返します そして私は見つけられませんコンバータを行う考え方 –
@MarcCollin以下のコードをフルコードで参照してください。 – ib11