Cstring to char * string

Source: Internet
Author: User

Instant messaging on Windows PlatformsProgramTo obtain the input string from the edit box and send it to the other end using the socket function. The project attribute is Unicode.

The string obtained from the edit box is cstring, And the socket function requires a char string. The conversion is a little troublesome. cstring itself does not provide this function.

The solution summarized after Tangle is as follows:

Non-Unicode project:

1. Obtain the memory address of the cstring storage string, which can be forcibly converted or copied directly.

Example:

Cstring strtemp;
Char sztemp [128];

Strtemp = _ T ("abckdkfei ");
Memset (sztemp, 0, sizeof (sztemp ));
Strcpy (sztemp, strtemp. getbuffer (0 ));


2. Use(Lpstr) (lpcstr) forced conversion

Example:

Char * Buf;

cstring STR =" hello ";
Buf = (lpstr) (lpctstr) STR;

The principles of the above two methods are based on the cstring class to save the char string. The first address of the internal string can be obtained to copy the data or directly convert the data.


Unicode Engineering

The preceding method in the Unicode project reports an error. It should be that wcahr cannot be directly converted to Char, so you can use the widechartomultibyte function to convert the above.

Example:

Cstring sendstring;
M_sendedit.getwindowtext (sendstring );
M_sendedit.setwindowtext (L "");

Char Buf [1024] = {'\ 0 '};
Widechartomultibyte (cp_acp, 0, sendstring. getbuffer (0), sendstring. getlength (), Buf, 1024,0, 0 );
Send (clientsocket, Buf, strlen (BUF), 0 );


3.Use the member function of the cstring class: oemtoansi. I did not verify this method. I checked the msdn explanation:

 

Cstring: oemtoansi

Visual Studio 6.0 this topic has not been rated-

Cstring: oemtoansi

Void oemtoansi ();

Remarks

Converts all the characters in this cstring object from the OEM character set to the ANSI character set. See the http://technet.microsoft.com/zh-cn/library/ms857349.aspx in the C ++ language reference.

This function is not available if _ Unicode is defined.


 

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.