Today with Android through the socket to send data to the computer using the C language to write the server, send English no problem, when the data into Chinese, the server received the data is garbled.
Suddenly thought. VS preprocessing uses ANSI encoding. and the Android network data is in UTF8 format. So the direct use of printf print out of course is garbled, so the solution is to convert the UFT8 format data into ansi!
Avoid encountering similar problems. Less detours, I now find their own transcoding function to paste out!
C language Encoding conversion function:
UTF8 turn to unicodewchar_t * Utf8tounicode (const char* str) {int Textlen = 0;wchar_t * Result;textlen = Multibytetowidech AR (Cp_utf8, 0, str,-1, null,0); result = (wchar_t *) malloc ((textlen+1) *sizeof (wchar_t)); Memset (result,0, (textlen+1) *sizeof (wchar_t)); MultiByteToWideChar (Cp_utf8, 0,str,-1, (LPWSTR) result,textlen); return result; } Unicode converted to Ansichar * Unicodetoansi (const wchar_t *STR) {char * result;int Textlen = 0;//wide char to multi Chartextlen = WideCharToMultiByte (CP_ACP, 0, str, -1, null, 0, NULL, NULL); result = (char *) malloc ((textlen+1) * sizeof (char)); memset (result, 0, sizeof (char) * (Textlen + 1)); WideCharToMultiByte (CP_ACP, 0, str,-1, result, Textlen, NULL, NULL); return result;}
So that when you receive the data, you can switch directly to the target code.
Android use socket to send Chinese, C language Server receive garbled problem resolution method