Convert string, cstring, tchar *, char * to the difference and link

Source: Internet
Author: User

Recently, the conversion of cstring, tchar *, and char * is used.

Find a detailed article. Reference it for your convenience.

Char is type. tchar is also! However, he can determine whether it is Char or w_char by defining a unicode macro;

Tchar is a string type that allows you to build in MBCS and unnicodeProgramYou can use the sameCode, You do not need to use tedious macro definitions to include your code, and char represents ASCII characters

# Ifdef Unicode
Typedef wchar_t tchar;
# Else
Typedef char tchar;
# Endif

Therefore, when MBCS is used for building, tchar is Char, and Unicode is used, tchar is wchar_t.
As for string, cstring is a type of operation that encapsulates string processing!
You can open theirSource codeFind out what it is!
Cstring belongs to the VC class library
String is a class library of standard C ++.
String. H is the library function of C.

It is better to study their source generation!

Tchar and Char are interchangeable types of the same Integer type.
String, cstring represents a memory area.

Tchar assignment

# Include <tchar. h>

Tchar szwindowtext [256] = _ T ("qq2006 ");

Char * And tchar * convert cstring
Cstring STR (****)

The following describes other conversions in detail.

View plaincopy to clipboardprint?
/*************************************** *******************************
* Function: transcstringtotchar
* Description: Convert cstring to tchar *
* Date:
**************************************** *******************************/
Tchar * cpublic: cstring2tchar (cstring & Str)
{
Int ilen = Str. getlength ();
Tchar * szrs = new tchar [ilen];
Lstrcpy (szrs, str. getbuffer (ilen ));
Str. releasebuffer ();
Return szrs;
}
/*************************************** *******************************
* Function: transcstringtotchar
* Description: Convert cstring to tchar *
* Date:
**************************************** *******************************/
Tchar * cpublic: cstring2tchar (cstring & Str)
{
Int ilen = Str. getlength ();
Tchar * szrs = new tchar [ilen];
Lstrcpy (szrs, str. getbuffer (ilen ));
Str. releasebuffer ();
Return szrs;
}

View plaincopy to clipboardprint?
/*************************************** ********************************
* Function: thcar2char
* Description: Convert tchar * To char *
* Date:
**************************************** *******************************
*/
Char * cpublic: thcar2char (tchar * tchstr)
{
Int ilen = 2 * wcslen (tchstr); // cstring. The length of a tchar character is not required.
Char * chrtn = new char [ilen + 1]
Wcstombs (chrtn, tchstr, ilen + 1); // If the conversion is successful, a non-negative value is returned.
Return chrtn;
}
/*************************************** ********************************
* Function: thcar2char
* Description: Convert tchar * To char *
* Date:
**************************************** *******************************
*/
Char * cpublic: thcar2char (tchar * tchstr)
{
Int ilen = 2 * wcslen (tchstr); // cstring. The length of a tchar character is not required.
Char * chrtn = new char [ilen + 1]
Wcstombs (chrtn, tchstr, ilen + 1); // If the conversion is successful, a non-negative value is returned.
Return chrtn;
}

View plaincopy to clipboardprint?
/*
**************************************** *******************************
* Function: cstring2char
* Description: Convert cstring to char *
* Date:
**************************************** *******************************
*/
Char * cpublic: cstring2char (cstring & Str)
{
Int Len = Str. getlength ();
Char * chrtn = (char *) malloc (LEN * 2 + 1) * sizeof (char); // the length of a Chinese character in a cstring is counted as one length.
Memset (chrtn, 0, 2 * Len + 1 );
Uses_conversion;
Strcpy (lpstr) chrtn, ole2a (Str. lockbuffer ()));
Return chrtn;
}
/*
**************************************** *******************************
* Function: cstring2char
* Description: Convert cstring to char *
* Date:
**************************************** *******************************
*/
Char * cpublic: cstring2char (cstring & Str)
{
Int Len = Str. getlength ();
Char * chrtn = (char *) malloc (LEN * 2 + 1) * sizeof (char); // the length of a Chinese character in a cstring is counted as one length.
Memset (chrtn, 0, 2 * Len + 1 );
Uses_conversion;
Strcpy (lpstr) chrtn, ole2a (Str. lockbuffer ()));
Return chrtn;
}

// reference
//// //
// Pocket PC unicode and ANSI string
// By Vassili Phillips, september 26,200 1.
// translated by Yang fangsiqi
/// //
View plaincopy to clipboardprint?
/*
******************************** ***********************************
* letter count: getansistring
* Description: converts a cstring (UNICODE) to a char * (ANSI)
* parameter: cstring & S the cstring to be converted
* return value: return the Conversion Result
* creation date:
* last modification:
************************************* *********************************
*/< BR> char * getansistring (const cstring & S)
{< br> int nsize = 2 * s. getlength ();
char * pansistring = new char [nsize + 1];
wcstombs (pansistring, S, nsize + 1);
return pansistring;
}

This article from csdn blog: http://blog.csdn.net/lsldd/archive/2009/08/05/4412531.aspx

The following are problems encountered during self-debugging:

/////////// The problem encountered when converting the Chinese characters stored in tchar * To char,

Tchar tszname [max_path] = text ("Liu pig ");

I always think that the length can be obtained by using nlen = _ tcslen (tszname) * sizeof (tchar );

During debugging, it is found that the converted characters are always less than one Chinese character. I don't understand why.

If you use

Int nlen = widechartomultibyte (cp_acp, 0, tszname,-1, null, 0, null, null );

Find the length of the character when actually converted to Char.

Allocate the memory space you actually need.

Char * pszname = new char [nlen * sizeof (char)];

Widechartomultibyte (cp_acp, 0, tszname,-1, pszname, nlen, null, null );

Use of multibytetowidechar:

Int nlen = multibytetowidechar (cp_acp, 0, pmultibytestr,-1, null, 0 );

Tchar * pwidechar = new tchar [nlen * sizeof (tchar)];

Multibytetowidechar (cp_acp, 0, pmultibytestr,-1, pwidechar, nlen );

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/lxpspring/archive/2009/10/29/4745474.aspx

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.