<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'Converting wchar_t to char???' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'Converting wchar_t to char???' posted on the 'Beginner C/C++' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2012 Programmers Heaven</copyright>
    <pubDate>Thu, 09 Feb 2012 09:19:36 -0800</pubDate>
    <lastBuildDate>Thu, 09 Feb 2012 09:19:36 -0800</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <image>
      <url>http://www.programmersheaven.com/images/ph.gif</url>
      <title>Programmers Heaven</title>
      <link>http://www.programmersheaven.com/</link>
      <width>88</width>
      <height>31</height>
    </image>
    <item>
      <title>Converting wchar_t to char???</title>
      <link>http://www.programmersheaven.com/mb/beginnercpp/361397/361397/converting-wchar_t-to-char/</link>
      <description>How do you convert wchar_t to char?&lt;br /&gt;
&lt;br /&gt;
because i get this error message:&lt;br /&gt;
error C2664: 'CComPort::Open' : cannot convert parameter 1 from 'wchar_t [4]' to 'char *'</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/beginnercpp/361397/361397/converting-wchar_t-to-char/</guid>
      <pubDate>Fri, 15 Jun 2007 12:24:19 -0800</pubDate>
      <category>Beginner C/C++</category>
    </item>
    <item>
      <title>Re: Converting wchar_t to char???</title>
      <link>http://www.programmersheaven.com/mb/beginnercpp/361397/361431/re-converting-wchar_t-to-char/#361431</link>
      <description>: How do you convert wchar_t to char?&lt;br /&gt;
: &lt;br /&gt;
: because i get this error message:&lt;br /&gt;
: error C2664: 'CComPort::Open' : cannot convert parameter 1 from &lt;br /&gt;
: 'wchar_t [4]' to 'char *'&lt;br /&gt;
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:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
wchar_t wide = /* something */;
assert(wide &amp;gt;= 0 &amp;amp;&amp;amp; wide &amp;lt; 256 &amp;amp;&amp;amp;);
char myChar = (char) wide; /* C style cast */
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
To convert a wchar_t array to a char pointer:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
wchar_t myArray[4] = /* something */;
char * myPointer = (char*) &amp;amp;myArray[0];
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
Good luck,&lt;br /&gt;
&lt;br /&gt;
bilderbikkel</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/beginnercpp/361397/361431/re-converting-wchar_t-to-char/#361431</guid>
      <pubDate>Sat, 16 Jun 2007 02:29:53 -0800</pubDate>
      <category>Beginner C/C++</category>
    </item>
    <item>
      <title>Re: Converting wchar_t to char???</title>
      <link>http://www.programmersheaven.com/mb/beginnercpp/361397/361441/re-converting-wchar_t-to-char/#361441</link>
      <description>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() &lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
wchar_t helloW[] = _TEXT("Hello World");
char helloA[20] = {0};
// now convert the string
wcstombs(helloA, helloW, wcslen(helloW)+1);
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;error C2664: 'CComPort::Open' : cannot convert parameter 1 from 'wchar_t [4]' to 'char *'&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=============================================&lt;br /&gt;
never lie -- the government doesn't like the competition. (Author unknown)</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/beginnercpp/361397/361441/re-converting-wchar_t-to-char/#361441</guid>
      <pubDate>Sat, 16 Jun 2007 05:46:41 -0800</pubDate>
      <category>Beginner C/C++</category>
    </item>
  </channel>
</rss>
