2017-05-27 104 views
0

我有一個Windows應用程序,在C #寫,有一個網絡監聽和PHP頁從mysql數據庫中獲取一個字符串,並通過套接字窗口發送字符串應用程序使用此代碼:獲取UTF8字符串

$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); 

當我收到在Windows應用程序中的字符串,有一樣的東西:

¨ÛستÙUU©Ø§Ø³ÙÙدÙزارÙØ ³ÛØμدÙÙUUOÙ¾Ùج

我嘗試將此代碼轉換爲UTF-8格式,在C#中這絲毫代碼

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

但它不會返回原始的字符串。

任何人都可以告訴我如何從我的PHP頁面發送原始字符串?

+0

你有一個網頁無效的html字符(請參閱:https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references)因此,使用網絡庫中的WebUtility:System.Net.WebUtility.HtmlDecode()或System.Net.WebUtility.HtmlEncode() – jdweng

+0

WebU tility.HtmlDecode返回確切的輸入字符串...關於無效的html字符,我將它設置爲utf8 ...你的意思是它必須是別的東西? –

+0

是的。 utf8只是將ASCII字符0-255轉換爲沒有轉換的字節數組。 WebUtility會在我提供的網頁鏈接上顯示轉換。 – jdweng

回答

0

您期待阿拉伯語?包括ست等?你有Mojibake。有關更多討論,請參閱this

不要使用任何編碼/解碼功能,它只會讓事情變得更糟。

您需要在連接是這樣的:

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

(或utf8mb4

如果事實證明你有「雙重編碼」,見this