Unicode columns in VC

Source: Internet
Author: User
In vc6.0, multi-byte encoding is used by default, and since vs2005, It is the default Unicode character encoding format. For ANSI, the difference between MBCS (Multi-byte encoding) and Unicode can be Baidu. The following blog post is also detailed:

Http://www.tongji.net/index.php/uid-160994-action-viewspace-itemid-12415

Generally speaking, we use the console written in C language.ProgramMost of them are multi-byte encoding. The English letter is a byte, and the Chinese character is two bytes. Unicode encoding occupies two bytes, both Chinese and English. One advantage of Unicode encoding in VC is that the interface made by MFC can be the interface of the Operating System (more interfaces can be used in vc2008 ). When using multi-byte encoding, the interface is the 98 style interface in vc6.0. This is only one reason. The more important reason is the unification of standards, so Unicode is necessary.

 

Before using vc2008 to write a program, add _ T to the front of the string, for example, _ T ("heheh! ") Indicates that the string will be taken into consideration during compilation." heheh! "Converted to Unicode or multi-character encoding format. L "heheh! "Means to set the string" heheh! "Convert to Unicode. Now there is a troublesome problem, that is, converting Unicode cstring to Char [], mainly encountered in the sendto function in socket programming.

Refer to a blog post: http://cj0000.itpub.net/post/4086/483973

LastCodeAs follows:

// ------------------------- Global function ---------------------------

// Converts Unicode cstring to a multi-byte character array.
String Widechartomultichar (wstring Str)
{
String Return_value;
// Obtain the buffer size and apply for space. The buffer size is calculated in bytes.
Int Len = Widechartomultibyte (cp_acp, 0 , Str. c_str (), str. Size (), null, 0 , Null, null );
Char   * Buffer =   New   Char [Len + 1 ];
Widechartomultibyte (cp_acp, 0 , Str. c_str (), str. Size (), buffer, Len, null, null );
Buffer [Len] =   ' \ 0 ' ;
// Delete the buffer and return values
Return_value.append (buffer );
Delete [] buffer;
Return Return_value;
}
// ------------------------- Global function ---------------------------

Void Cchatdlg: onbnclickedbuttonsend ()
{
// Todo: add the control notification handler code here
// Obtain the peer IP Address
DWORD dwip;
(Cipaddressctrl * ) Getdlgitem (idc_ipaddress1 )) -> Getaddress (dwip );

Sockaddr_in addrto;
Addrto. sin_family=Af_inet;
Addrto. sin_port=Htons (6000);
Addrto. sin_addr.s_un.s_addr=Htonl (dwip );

Cstring strsend;
String Tempsend;
// Get data to be sent
Getdlgitemtext (idc_edit_send, strsend );
// Convert cstring to char * in Unicode *
Tempsend = Widechartomultichar (lpctstr) strsend );
// Send data
Sendto (m_socket, tempsend. c_str (), tempsend. Length () + 1 , 0 , (Sockaddr * ) & Addrto, Sizeof (Sockaddr ));
// Clear the content of the sending box
Setdlgitemtext (idc_edit_send, _ T ( "" ));
}

During conversion, the number of characters in the string and the ending symbol of the string are important.

The final operation is successful.

In addition, there is an article on msdnArticleAlso good, you can refer to: http://msdn.microsoft.com/en-us/library/ms235631.aspx

Another method is to convert cstring to char *, while the previous method can only convert cstring to const char * through string. c_str *.

First add the header file: # include <afxconv. h>

Cstring strsend;
Int Len;
Getdlgitemtext (idc_edit_send, strsend );

# Ifdef _ Unicode
// Cstring to char *
Uses_conversion;
Wsabuf. Buf = W2a (strsend );
Wsabuf. Len = Strlen (wsabuf. BUF) + 1 ;
# Else
Len = Strsend. getlength ();
Wsabuf. Buf = Strsend. getbuffer (LEN );
Wsabuf. Len = Len + 1 ;
# Endif

It is convenient to convert char * To cstring:

Char Buf [200];

Cstring STR (BUF );

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.