Android use socket to send Chinese, C language Server receive garbled problem resolution method

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.