Cstring to char type conversion --- "=": cannot be converted from "wchar_t *" to "char *

Source: Internet
Author: User

This article has been copied from the Internet and the source of the original article has been lost. Sorry.
In VC 2005, this simple problem is a little more complicated.
In the project, an essential step is to convert the cstring to the shar * string. Through Google, I found that the following methods can be used:
Use the getbuffer method of cstring
Cstring S ("Hello, world ");
Char * c = S. getbuffer (0 );
However, I compiled the following information in VC ++ 2005:
Error c2440: "initialization": cannot be converted from "wchar_t *" to "char *"
The difference between wchar_t and char is not very clear. In msdn, we checked that wchar_t is a wide character type, which is equivalent to unsigned short (16bit ). The Char we usually use is 8bit. Continue to search for conversion from wchar_t * To char *. The previous article on msdn is convert between various string types, which describes various strings char *, wchar_t *, _ bstr_t in VC ++ 2005, ccombstr, cstring, basic_string, and system. string Conversion. The code for converting wchar_t * To char * is as follows: (To maintain Article consistency, the variable name is modified)
# Include <stdlib. h>
# Include <iostream>
Using namespace STD;
Int main ()
{
Wchar_t * s = l "Hello, world ";
Wcout <S <Endl;

// Convert to a char *
Size_t S0 = wcslen (s) + 1;
Const size_t newsize = 100;
Size_t convertedchars = 0;
Char C [newsize];
Wcstombs_s (& convertedchars, C, S0, S, _ truncate );
Cout <C <Endl;
}
The output is correct. All are Hello, world!

As for why the original code can be used by others, I cannot directly use it under VC ++ 2005, but what should I do through conversion? We can see that chapter 2 of programming windows talks about Unicode and asks related questions in the msdn forum to get the answer.
Previously, before VC ++ 2005, Unicode support was disabled for applications by default. In vc2005, support for Unicode was enabled by default. The string corresponding to cstring should be tchar, tchar is defined as follows,
# Ifdef _ Unicode
Typedef wchar_t tchar;
# Else
Typedef char tchar;
# Endif
I think this is the reason why I cannot directly convert in VC ++ 2005. In the project, you should be able to disable Unicode support for direct conversion. In this case, right-click the project name-> property-> character set in general and select not set. In this way, the code at the beginning can be correctly executed. This saves a lot of trouble.

Cstring to char type conversion --- "=": cannot be converted from "wchar_t *" to "char *

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.