2

I have a string extracted from a url as filename that contains special Turkish characters (çğıİöüş) and they seem wrong. How can I fix it?

public static string getFileName(HttpWebResponse response, string url)
{
    var cd = response.Headers["content-disposition"];
    var loc = response.Headers["location"];

    if (!string.IsNullOrEmpty(cd))
    {
        var disp = ContentDispositionHeaderValue.Parse(cd);
        return Uri.UnescapeDataString(disp.FileName);
    }
    else if (!string.IsNullOrEmpty(loc))
        return Path.GetFileName(loc);
    else
        return Path.GetFileName(url);
}

Original String:

y2mate.com - Cengiz Özkan - Suzan Suzi (Kırklar Dağının Düzü)_VaW6Mhde9Ko.mp3

Correct string:

y2mate.com - Cengiz Özkan - Suzan Suzi (Kırklar Dağının Düzü)_VaW6Mhde9Ko.mp3
Ali Tor
  • 2,482
  • 2
  • 24
  • 52
  • How are you receiving this string? A .Net string as such can handle these special characters without any problem. The root cause is in the way you retrieve/receive this string value and you'll have to add a [mcve] of this part of your code. – Filburt Jul 13 '20 at 18:58
  • @Filburt, I have updated adding detail – Ali Tor Jul 13 '20 at 19:01
  • Found that in case if: [Encoding trouble with HttpWebResponse](https://stackoverflow.com/questions/227575/encoding-trouble-with-httpwebresponse) –  Jul 13 '20 at 19:13

1 Answers1

3

It seems you've mixed Win-1254 and Utf-8 encodings:

string original =
  @"y2mate.com - Cengiz Özkan - Suzan Suzi (Kırklar Dağının Düzü)_VaW6Mhde9Ko.mp3";

string correct = Encoding.UTF8.GetString(Encoding.GetEncoding(1254).GetBytes(original));

// Let's have a look
Console.Write(correct);

Outcome:

y2mate.com - Cengiz Özkan - Suzan Suzi (Kırklar Dağının Düzü)_VaW6Mhde9Ko.mp3
Dmitry Bychenko
  • 165,109
  • 17
  • 150
  • 199
  • @Olivier Rogier: We have a *special* encoding for Turkish - it's `Win-1254` so I've started from it; `à => Ö`, `ı => ü` pattern (note `Ä` one an the same character for each Turkish char) is typical for Utf-8 misencoding. So I had a good candidate to try – Dmitry Bychenko Jul 13 '20 at 19:20