VC Programming Unicode Character set convert char array to CString

Source: Internet
Author: User

struct sockaddr_in addr;
int addr_len = sizeof (struct sockaddr_in);

CString strstate;
Remember to use char to receive data, because the data transmitted in the network is just and encoded


Irrelevant stream of bytes
So the byte stream is received and converted to wide-character Unicode encoding for display and


Write a file
Char tempsocketreceive[maxdatalength] = "";
memset (tempsocketreceive, 0, maxdatalength);//char empty;
Tempsocketreceive[maxdatalength-1] = ' + ';//Prevent data end


There is hot and hot scalding, plus it still appears
int length = Recvfrom (Listen_socket, Tempsocketreceive,


Maxdatalength, 0, (struct sockaddr *) &addr, &addr_len);


Determine the amount of memory space required to convert multibyte encoding to wide-byte encoding
DWORD dwnum = MultiByteToWideChar (CP_ACP, 0,


Tempsocketreceive,-1, NULL, 0);
Allocate memory on the heap, the requested memory must be released, or the memory leaks in the morning


Cause the program to crash
wchar_t *pwtext;
Pwtext = new Wchar_t[dwnum];
Converts a byte-wide word to Unicode, and the result of the conversion is stored on the heap memory, remembering


Delete
MultiByteToWideChar (CP_ACP, 0, Tempsocketreceive,-1,


Pwtext, Dwnum);
The received data is expressed in CString, which facilitates display on MFC and writing with CFile


Thing

CString RawData (Pwtext);//raw serial data data without parsing
CString temp = Comdecode (rawdata);//parsed serial port data
Heap memory has been converted to stack memory by the CString constructor, so the heap memory is released early


Put
Delete[]pwtext;

VC Programming Unicode Character set convert char array to CString

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.