, 내가받을 인용 - 인쇄 "Chasn = C3 = A9 쉬르 illet" 내가 "을 Chasné 쉬르 illet"를 좀하고 싶습니다 변환하는 방법 및 나는 평균 2 일 웹 검색을 어떤 해결책을 찾을 수 없습니다.문자열
C# ou VB.NET 누구든지 나를 도와 줄 수 있습니까?
감사합니다.
, 내가받을 인용 - 인쇄 "Chasn = C3 = A9 쉬르 illet" 내가 "을 Chasné 쉬르 illet"를 좀하고 싶습니다 변환하는 방법 및 나는 평균 2 일 웹 검색을 어떤 해결책을 찾을 수 없습니다.문자열
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é surillet"입니다. 코드는 각 16 진수를 별도의 문자로 취급하지만 "= C3 = A9"는 단일 문자 "é"를 나타냅니다. – 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
매개 변수를 가져와야합니다.
물론 ... 그 정보를 얻는 방법을 모른다면, 내 awesome MailKit 라이브러리를 사용하여 IMAP에서 MIME 부분을 가져와이 모든 작업을 수행 할 수 있습니다.
우리는이 방법에 문제가있어 - 매우 느립니다. 다음 성능 향상 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 명령과 함께 본문 텍스트 가져 오기 : FETCH BODY [TEXT] 그리고 나에게 Quoted_printable 형식으로 코딩 된 문자열을 반환합니다. 그리고 찾을 수 없습니다. 변환기를 할 수있는 아이디어 –
@MarcCollin 전체 코드와 함께 아래 코드를 참조하십시오. – ib11