Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5058
Number of posts: 16232

This Forum Only
Post New Thread

Report
Converting wchar_t to char??? Posted by weiopjfgiptyq3 on 15 Jun 2007 at 12:24 PM
How do you convert wchar_t to char?

because i get this error message:
error C2664: 'CComPort::Open' : cannot convert parameter 1 from 'wchar_t [4]' to 'char *'
Report
Re: Converting wchar_t to char??? Posted by bilderbikkel on 16 Jun 2007 at 2:29 AM
: How do you convert wchar_t to char?
:
: because i get this error message:
: error C2664: 'CComPort::Open' : cannot convert parameter 1 from
: 'wchar_t [4]' to 'char *'
You try to convert a wchar_t array to a char pointer. Be carefull wit this, as a wchar_t typically can hold more values than a char, so in the cast you might lose information! You can check this e.g. like this:

wchar_t wide = /* something */;
assert(wide >= 0 && wide < 256 &&);
char myChar = (char) wide; /* C style cast */


To convert a wchar_t array to a char pointer:

wchar_t myArray[4] = /* something */;
char * myPointer = (char*) &myArray[0];


Good luck,

bilderbikkel
Report
Re: Converting wchar_t to char??? Posted by stober on 16 Jun 2007 at 5:46 AM
In most cases you can not make that conversion by simple typecasting as bilderbikkel is suggesting. You have to use one of several conversion functions, such as wcstombs()

wchar_t helloW[] = _TEXT("Hello World");
char helloA[20] = {0};
// now convert the string
wcstombs(helloA, helloW, wcslen(helloW)+1);


>>error C2664: 'CComPort::Open' : cannot convert parameter 1 from 'wchar_t [4]' to 'char *'

Depending on how you coded it you might be able to simply use one of the UNICODE macros as I did in the HelloWorldW example above. Just surround the literal text with the _TEXT macro. It only works with literals, not character arrays.

=============================================
never lie -- the government doesn't like the competition. (Author unknown)



 
Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2010 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
bootstrapLabs Logo A BootstrapLabs project.