0

I have a html text that provides from a server. but persian letters does not appears correctly. for example like this :

جشن نوروز/جشن سال نو

I have used this extension method from https://stackoverflow.com/a/11300580 to decode this text

public static string DecodeFromUtf8(this string utf8String)
{
    // copy the string as UTF-8 bytes.
    byte[] utf8Bytes = new byte[utf8String.Length];
    for (int i = 0; i < utf8String.Length; ++i)
    {
         utf8Bytes[i] = (byte)utf8String[i];
    }

    return Encoding.UTF8.GetString(utf8Bytes, 0, utf8Bytes.Length);
}

but It will return this :

جش�  � ��ر��ز/جش�  سا�\u001e � ��

I am using .net framework 4.5 . if anyone help me how to crack this I would be appreciated.

  • 2
    What viewer are you using? It looks like you are viewing in a console window and the console is not set to the correct encoding. So the code is correct, just need to view with correct settings. – jdweng Jun 29 '20 at 12:40
  • @jdweng I did not print out any thing. I just want save it to database. I notice that when I was debug my code – farshid torkaman Jun 29 '20 at 12:43
  • @Crowcoder Yes it was. thanks – farshid torkaman Jun 29 '20 at 12:45
  • The string is probably correct and just while debugging VS was showing data in default encoding. See following : https://stackoverflow.com/questions/696627/how-to-set-standard-encoding-in-visual-studio – jdweng Jun 29 '20 at 12:48
  • The number of bytes in a utf8 encoded string is not equal to the number of characters. For starters a char is 2 bytes. – Hans Kesting Jun 29 '20 at 15:32

0 Answers0