请问如何将wchar_t*类型转换成char*类型?

ingame 2001-06-26 09:09:04
请问如何将wchar_t*类型转换成char*类型?
...全文
234 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
飞行兔子 2001-06-27
  • 打赏
  • 举报
回复
WideCharToMultiByte( CP_ACP,
0,
lpWideCharStr, // 要转换的wchar_t*
-1,
lpMultiByteStr, // 接收char*的缓冲区指针
cbMultiByte, // lpMultiByteStr的缓冲区的大小
NULL,
NULL );
zeroweb 2001-06-27
  • 打赏
  • 举报
回复
用WideCharToMultiByte,具体方法看帮助 :)
gpmn 2001-06-27
  • 打赏
  • 举报
回复
sorry,搞错了,开始发了一堆废物,不过后面的东西很有用哦,呵呵
gpmn 2001-06-27
  • 打赏
  • 举报
回复
String Conversion Macros
The string conversion macros discussed here are valid for both ATL and MFC. For more information on MFC string conversion, see TN059: Using MFC MBCS/Unicode Conversion Macros and MFC Macros and Globals.

The syntax of the string-conversion macros is:

MACRONAME( string_address )

For example:

A2W(lpa)

In the macro names, the source string type is on the left (for example, A) and the destination string type is on the right (for example, W). A stands for LPSTR, OLE stands for LPOLESTR, T stands for LPTSTR, and W stands for LPWSTR.

Thus, A2W converts an LPSTR to an LPWSTR, OLE2T converts an LPOLESTR to an LPTSTR, and so on.

The destination string is created using _alloca, except when the destination type is BSTR. Using _alloca allocates memory off the stack, so that when your function returns, it is automatically cleaned up.

If there is a C in the macro name, the macro converts to a const string. For example, W2CA converts an LPWSTR to an LPCSTR.

Note When using an ATL string conversion macro, specify the USES_CONVERSION macro at the beginning of your function in order to avoid compiler errors. For example:

void func( LPSTR lpsz )
{
USES_CONVERSION;
...
LPWSTR x = A2W(lpsz)
// Do something with x
...
}

The behavior of the ATL string conversion macros depends on the compiler directive in effect, if any. If the source and destination types are the same, no conversion takes place. Compiler directives change T and OLE as follows:

Compiler directive in effect T becomes OLE becomes
none A W
_UNICODE W W
OLE2ANSI A A
_UNICODE and OLE2ANSI W A


The following table lists the ATL string conversion macros.

ATL String Conversion Macros

A2BSTR OLE2A T2A W2A
A2COLE OLE2BSTR T2BSTR W2BSTR
A2CT OLE2CA T2CA W2CA
A2CW OLE2CT T2COLE W2COLE
A2OLE OLE2CW T2CW W2CT
A2T OLE2T T2OLE W2OLE
A2W OLE2W T2W W2T


ATL Macros and Global Functions

See Also

DEVMODE and TEXTMETRIC String Conversion Macros


--------------------------------------------------------------------------------
Send feedback to MSDN.Look here for MSDN Online resources.
gpmn 2001-06-27
  • 打赏
  • 举报
回复
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML dir=ltr>
<HEAD>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=Windows-1252">
<title>String Conversion Macros</title>

<!--CSS_START-->
<script language="JavaScript" src='MS-ITS:dsmsdn.chm::/html/msdn_header.js'></script>
<!--CSS_END-->
</HEAD>
<BODY>
<font face="verdana,arial,helvetica" size="2">
<h1><a name="_atl_string_conversion_macros"></a>String Conversion Macros</h1>
<p>
The string conversion macros discussed here are valid for both ATL and MFC. For more information on MFC string conversion, see <a href="_mfcnotes_tn059.htm">TN059: Using MFC MBCS/Unicode Conversion Macros</a> and <a href="_mfc_mfc_macros_and_globals.htm">MFC Macros and Globals</a>.</p>
<p>
The syntax of the string-conversion macros is:</p>
<p>
<b>MACRONAME( </b><i>string_address </i><b>)</b></p>
<p>
For example:</p>
<p>
<b>A2W(</b><i>lpa</i><b>)</b></p>
<p>
In the macro names, the source string type is on the left (for example, <b>A</b>) and the destination string type is on the right (for example, <b>W</b>). <b>A</b> stands for <b>LPSTR</b>, <b>OLE</b> stands for <b>LPOLESTR</b>, <b>T</b> stands for <b>LPTSTR</b>, and <b>W</b> stands for <b>LPWSTR</b>.</p>
<p>
Thus, <b>A2W</b> converts an <b>LPSTR</b> to an <b>LPWSTR</b>, <b>OLE2T</b> converts an <b>LPOLESTR</b> to an <b>LPTSTR</b>, and so on.</p>
<p>
The destination string is created using _<b>alloca</b>, except when the destination type is <b>BSTR</b>. Using _<b>alloca</b> allocates memory off the stack, so that when your function returns, it is automatically cleaned up.</p>
<p>
If there is a <b>C</b> in the macro name, the macro converts to a <b>const</b> string. For example, <b>W2CA</b> converts an <b>LPWSTR</b> to an <b>LPCSTR</b>.</p>
<p class=indent>
<B><b>Note</b></B>   When using an ATL string conversion macro, specify the <b>USES_CONVERSION</b> macro at the beginning of your function in order to avoid compiler errors. For example:</p>
<pre><code>void func( LPSTR lpsz )
{
  USES_CONVERSION;
  ...
  LPWSTR x = A2W(lpsz)
  // Do something with x
  ...
}
</code></pre>
<p>
The behavior of the ATL string conversion macros depends on the compiler directive in effect, if any. If the source and destination types are the same, no conversion takes place. Compiler directives change <b>T</b> and <b>OLE</b> as follows:</p>
<table border=1 cellpadding=5 cols=3 frame=below rules=rows>
<tr valign=top>
<td class=label width=44%><b>Compiler directive in effect</b></td>
<td class=label width=33%><b>T becomes</b></td>
<td class=label width=23%><b>OLE becomes</b></td>
</tr>
<tr valign=top>
<td width=44%>none</td>
<td width=33%><b>A</b></td>
<td width=23%><b>W</b></td>
</tr>
<tr valign=top>
<td width=44%><b>_UNICODE</b></td>
<td width=33%><b>W</b></td>
<td width=23%><b>W</b></td>
</tr>
<tr valign=top>
<td width=44%><b>OLE2ANSI</b></td>
<td width=33%><b>A</b></td>
<td width=23%><b>A</b></td>
</tr>
<tr valign=top>
<td width=44%><b>_UNICODE</b> and <b>OLE2ANSI</b></td>
<td width=33%><b>W</b></td>
<td width=23%><b>A</b></td>
</tr>
</table><br>
<p>
The following table lists the ATL string conversion macros.</p>
<p class=label>
<b>ATL String Conversion Macros</b></p>
<table border=1 cellpadding=5 cols=4 frame=below rules=rows>
<tr valign=top>
<td width=25%><b>A2BSTR</b></td>
<td width=25%><b>OLE2A</b></td>
<td width=25%><b>T2A</b></td>
<td width=25%><b>W2A</b></td>
</tr>
<tr valign=top>
<td width=25%><b>A2COLE</b></td>
<td width=25%><b>OLE2BSTR</b></td>
<td width=25%><b>T2BSTR</b></td>
<td width=25%><b>W2BSTR</b></td>
</tr>
<tr valign=top>
<td width=25%><b>A2CT</b></td>
<td width=25%><b>OLE2CA</b></td>
<td width=25%><b>T2CA</b></td>
<td width=25%><b>W2CA</b></td>
</tr>
<tr valign=top>
<td width=25%><b>A2CW</b></td>
<td width=25%><b>OLE2CT</b></td>
<td width=25%><b>T2COLE</b></td>
<td width=25%><b>W2COLE</b></td>
</tr>
<tr valign=top>
<td width=25%><b>A2OLE</b></td>
<td width=25%><b>OLE2CW</b></td>
<td width=25%><b>T2CW</b></td>
<td width=25%><b>W2CT</b></td>
</tr>
<tr valign=top>
<td width=25%><b>A2T</b></td>
<td width=25%><b>OLE2T</b></td>
<td width=25%><b>T2OLE</b></td>
<td width=25%><b>W2OLE</b></td>
</tr>
<tr valign=top>
<td width=25%><b>A2W</b></td>
<td width=25%><b>OLE2W</b></td>
<td width=25%><b>T2W</b></td>
<td width=25%><b>W2T</b></td>
</tr>
</table><br>
<p>
<a href="_atl_atl_macros_and_global_functions.htm">ATL Macros and Global Functions</a></p>
<p class=label>
<b>See Also</b></p>
<p>
<a href="_atl_devmode_and_textmetric_string_conversion_macros.htm">DEVMODE and TEXTMETRIC String Conversion Macros</a></p>
</font>
<!--FOOTER_START-->
<script language="JavaScript" src='MS-ITS:dsmsdn.chm::/html/msdn_footer.js'></script>
<!--FOOTER_END-->
</BODY>
</HTML>
Chice_wxg 2001-06-26
  • 打赏
  • 举报
回复
用WideCharToMultiByte,具体方法看帮助 :)
huangbeyond 2001-06-26
  • 打赏
  • 举报
回复
指针与数据类型无关呀!

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧