0

I have a Windows application that write in C# and have a network listener and a PHP page that get a string from mysql database and send that string via a socket to windows application with this code:

$remote_ip='192.168.5.133';//this is the ip of computer that run windows app
ini_set('display_errors',off);
$sh=fsockopen($remote_ip,6000,$en,$en,1);
ini_set('display_errors',on);

fputs($sh,$Text_To_Send_Windows_Application);

$test_keycard=fread($sh,2);
fclose($sh);

When I receive the string in windows application, there is something like :

¨Ûست Ù ÛÚ© اسÙÙØ¯ ÙØ²Ø§Ø± Ù Ø³ÛØµØ¯ Ù ÙÙØ¯ Ù Ù¾ÙØ¬

I try to convert this code to utf8 format in C# whit this code

string plainText = UTF8Encoding.UTF8.GetString(UTF8encodes);

But it not return original string.

Could anyone tell me how I get original string that send from my PHP page?

Ali
  • 3,373
  • 5
  • 42
  • 54
  • You have a webpage with invalid html characters (see : https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references) So use WebUtility from Net Library : System.Net.WebUtility.HtmlDecode() or System.Net.WebUtility.HtmlEncode() – jdweng May 27 '17 at 09:00
  • WebUtility.HtmlDecode return exactly the input string...about invalid html characters i set it to utf8...do you mean it must be somethings else? – Ali May 27 '17 at 09:09
  • Yes. utf8 just converts ASCII character 0-255 to byte array with no conversion. WebUtility does the conversion show on webpage link I provided. – jdweng May 27 '17 at 09:28
  • then i must convert string in win app yeah? – Ali May 27 '17 at 09:37
  • If the win app is receiving html then yes. – jdweng May 27 '17 at 11:42

1 Answers1

0

You are expecting Arabic? Including ست etc? You have Mojibake. See this for more discussion.

Do not use any encoding/decoding functions, it only makes things worse.

You need something like this in the connection:

id=my_user;password=my_password;database=some_db123;charset=utf8;

(or utf8mb4)

If it turns out that you have "double encoding", see this

Rick James
  • 135,179
  • 13
  • 127
  • 222