Conversion between cstring and char and byte

Source: Internet
Author: User

In VC, cstring classes have various usage forms and are rich and colorful, but sometimes the conversion from traditional data types has become a challenge for many people.

Next I will briefly introduce the relationships between them.

1. How to convert cstring to char:

Cstring m_cstr = "ABC ";
Char * m_char = "def ";


Char * Lp = M_cstr.getbuffer (m_cstr.getlength ());  
M_cstr.releasebuffer ();

// Convert the opposite char * To cstring
M_cstr.format ( " % S " , LP); // LP is an array pointer



 

Type 2: Forced type conversion to lpctstr
Lptstr P = M_cstr.getbuffer ( 10 );
Strcpy (count, P ); M_cstr.releasebuffer ();

 

2. Conversion between cstring and byte

Byet is unsigned character type. The principle is similar to that of char.

 

Cstring m_cstr;
Byte m_byte [ 100 ];
M_byte = (Byte * ) M_cstr.getbuffer (m_cstr.getlength ()); // Cstring to byte

Likewise:
M_cstr.format ( " % S " , M_byte ); // Byte string output

3 Conversion between byte and char type

Strcpy and memcpy functions can be forcibly converted.

Strcpy (m_char, (char *) m_byte );

Strlen = (char *) m_byet;

memcpy (m_char, m_byte, strlen);

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.