Кодировка Quoted-Printable UTF-8 - C#
Формулировка задачи:
Здравствуйте ! Пытаюсь раскодировать Quoted-Printable строку... код работает нормально для любой кодировки, кроме UTF-8, я не знаю, почему он не может декодировать UTF-8. Как декодировать Quoted-Printable строку UTF-8 (кириллица) в нормальный читаемый вид?
Листинг программы
- public static string DecodeEncodedWordValue(string mimeString)
- {
- var regex = new Regex(@"=\?(?<charset>.*?)\?(?<encoding>[qQbB])\?(?<value>.*?)\?=");
- var encodedString = mimeString;
- var decodedString = string.Empty;
- while (encodedString.Length > 0)
- {
- var match = regex.Match(encodedString);
- if (match.Success)
- {
- // If the match isn't at the start of the string, copy the initial few chars to the output
- decodedString += encodedString.Substring(0, match.Index);
- var charset = match.Groups["charset"].Value;
- var encoding = match.Groups["encoding"].Value.ToUpper();
- var value = match.Groups["value"].Value;
- if (encoding.Equals("B"))
- {
- // Encoded value is Base-64
- var bytes = Convert.FromBase64String(value);
- decodedString += Encoding.GetEncoding(charset).GetString(bytes);
- }
- else if (encoding.Equals("Q"))
- {
- // Encoded value is Quoted-Printable
- // Parse looking for =XX where XX is hexadecimal
- var regx = new Regex("(\\=([0-9A-F][0-9A-F]))", RegexOptions.IgnoreCase);
- decodedString += regx.Replace(value, new MatchEvaluator(delegate(Match m)
- {
- 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);
- }
- return Encoding.GetEncoding(charset).GetString(bytes);
- }));
- decodedString = decodedString.Replace('_', ' ');
- }
- else
- {
- // Encoded value not known, return original string
- // (Match should not be successful in this case, so this code may never get hit)
- decodedString += encodedString;
- break;
- }
- // Trim off up to and including the match, then we'll loop and try matching again.
- encodedString = encodedString.Substring(match.Index + match.Length + 1);
- }
- else
- {
- // No match, not encoded, return original string
- decodedString += encodedString;
- break;
- }
- }
- return decodedString;
- }
Решение задачи: «Кодировка Quoted-Printable UTF-8»
textual
Листинг программы
- public static string DecodeEncodedWordValue(string mimeString) // перевод темы и отправителя письма из кодировки quoted printable
- {
- var regex = new Regex(@"=\?(?<charset>.*?)\?(?<encoding>[qQbB])\?(?<value>.*?)\?=");
- var encodedString = mimeString;
- var decodedString = string.Empty;
- while (encodedString.Length > 0)
- {
- var match = regex.Match(encodedString);
- if (match.Success)
- {
- decodedString += encodedString.Substring(0, match.Index);
- var charset = match.Groups["charset"].Value;
- var encoding = match.Groups["encoding"].Value.ToUpper();
- var value = match.Groups["value"].Value;
- if (encoding.Equals("B"))
- {
- // Encoded value is Base-64
- var bytes = Convert.FromBase64String(value);
- decodedString += Encoding.GetEncoding(charset).GetString(bytes);
- }
- else if (encoding.Equals("Q"))
- {
- // Encoded value is Quoted-Printable
- // Parse looking for =XX where XX is hexadecimal
- var regx = new Regex("(\\=([0-9A-F][0-9A-F]))", RegexOptions.IgnoreCase);
- decodedString += regx.Replace(value, new MatchEvaluator(delegate(Match m)
- {
- 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);
- }
- return Encoding.GetEncoding(charset).GetString(bytes);
- }));
- decodedString = decodedString.Replace('_', ' ');
- }
- else
- {
- // Encoded value not known, return original string
- // (Match should not be successful in this case, so this code may never get hit)
- decodedString += encodedString;
- break;
- }
- // Trim off up to and including the match, then we'll loop and try matching again.
- encodedString = encodedString.Substring(match.Index + match.Length);
- }
- else
- {
- // No match, not encoded, return original string
- decodedString += encodedString;
- break;
- }
- }
- return decodedString;
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д