﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-C++世界-随笔分类-数据-常用操作</title><link>http://www.cppblog.com/fdsajhg/category/14587.html</link><description>管理系统--&lt;</description><language>zh-cn</language><lastBuildDate>Sun, 12 Sep 2010 19:40:56 GMT</lastBuildDate><pubDate>Sun, 12 Sep 2010 19:40:56 GMT</pubDate><ttl>60</ttl><item><title>Windows API GB2312/Unicode/UTF-8转换</title><link>http://www.cppblog.com/fdsajhg/archive/2010/09/03/125770.html</link><dc:creator>傅先生</dc:creator><author>傅先生</author><pubDate>Fri, 03 Sep 2010 03:28:00 GMT</pubDate><guid>http://www.cppblog.com/fdsajhg/archive/2010/09/03/125770.html</guid><wfw:comment>http://www.cppblog.com/fdsajhg/comments/125770.html</wfw:comment><comments>http://www.cppblog.com/fdsajhg/archive/2010/09/03/125770.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fdsajhg/comments/commentRss/125770.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fdsajhg/services/trackbacks/125770.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: BYTE to Unicode//////////////BYTE dwByte[200];wchar_t dwcharw[200];for(int i=0;i&lt;200;i++){dwcharw[i]=dwByte[i];}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&...&nbsp;&nbsp;<a href='http://www.cppblog.com/fdsajhg/archive/2010/09/03/125770.html'>阅读全文</a><img src ="http://www.cppblog.com/fdsajhg/aggbug/125770.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fdsajhg/" target="_blank">傅先生</a> 2010-09-03 11:28 <a href="http://www.cppblog.com/fdsajhg/archive/2010/09/03/125770.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title> ansi-unicode --用法.</title><link>http://www.cppblog.com/fdsajhg/archive/2010/09/03/125765.html</link><dc:creator>傅先生</dc:creator><author>傅先生</author><pubDate>Fri, 03 Sep 2010 03:07:00 GMT</pubDate><guid>http://www.cppblog.com/fdsajhg/archive/2010/09/03/125765.html</guid><wfw:comment>http://www.cppblog.com/fdsajhg/comments/125765.html</wfw:comment><comments>http://www.cppblog.com/fdsajhg/archive/2010/09/03/125765.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fdsajhg/comments/commentRss/125765.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fdsajhg/services/trackbacks/125765.html</trackback:ping><description><![CDATA[<div class=title xmlns:asp="http://msdn2.microsoft.com/asp">strlen, strlen_l, wcslen, wcslen_l, _mbslen, _mbslen_l, _mbstrlen, _mbstrlen_l&nbsp; </div>
<div class=lw_vs>
<div id=curversion><strong>Visual Studio 2005</strong></div>
<div class=cl_lw_vs_seperator id=vsseperator></div>
<div id=versionclick onclick=ChangeVersionClick()>
<div id=versionclick_c1></div>
<div id=versionclick_c2>
<div class=""><a id=vsLink href="javascript:;"><font color=#960bb4>Other Versions </font></a></div>
<div class="cl_vs_arrow clip10x10" id=vsArrow><font color=#960bb4><img class=cl_lw_vs_arrow src="http://i3.msdn.microsoft.com/Hash/33169f5f6ffee1bf41ee6eb0b266ef57.png"></font></div>
</div>
<div id=versionclick_c3><font color=#960bb4></font></div>
</div>
<ul id=vsPanel>
    <li><a title="" href="http://msdn.microsoft.com/en-us/library/78zh94ax(v=VS.100).aspx"><font color=#1364c4>Visual Studio 2010</font></a>
    <li><a title="" href="http://msdn.microsoft.com/en-us/library/78zh94ax(v=VS.90).aspx"><font color=#1364c4>Visual Studio 2008</font></a>
    <li><a title="" href="http://msdn.microsoft.com/en-us/library/78zh94ax(v=VS.71).aspx"><font color=#1364c4>Visual Studio .NET 2003</font></a></li>
</ul>
</div>
<div id=mainSection>
<div id=mainBody>
<p><font color=#1364c4></font>
<p>Get the length of a string, using the current locale or a specified locale. More secure versions of these functions are available; see <span class=linkTerms><a href="http://msdn.microsoft.com/en-us/library/z50ty2zh(v=VS.80).aspx"><font color=#1364c4>strnlen, strnlen_l, wcsnlen, wcsnlen_l, _mbsnlen, _mbsnlen_l, _mbstrnlen, _mbstrnlen_l</font></a></span></p>
<div class=section id=syntaxSection>
<div class=LW_CodeSnippetContainer xmlns=""><a name=CodeSpippet0></a>
<div class=LW_CodeSnippetContainerCodeCollection>
<div class=LW_CodeSnippetToolBar>
<div class=LW_CodeSnippetToolBarText style="valign: top"><a title="Copy to clipboard." href="javascript:CodeSnippet_CopyCode('CodeSnippetContainerCode0');"><font color=#1364c4 size=2>Copy</font></a></div>
</div>
<div class=LW_CodeSnippetContainerCode id=CodeSnippetContainerCode0>
<div style="COLOR: black">
<pre>size_t strlen(
const char *str
);
size_t strlen_l(
const char *str,
_locale_t locale
);
size_t wcslen(
const wchar_t *str
);
size_t wcslen_l(
const wchar_t *str,
_locale_t locale
);
size_t _mbslen(
const unsigned char *str
);
size_t _mbslen_l(
const unsigned char *str,
_locale_t locale
);
size_t _mbstrlen(
const char *str
);
size_t _mbstrlen_l(
const char *str,
_locale_t locale
);
</pre>
</div>
</div>
</div>
</div>
</div>
<p>
<h4 class=subHeading>Parameters</h4>
<dl>
<dt><span class=nonLinkTerms><span class=parameter>str</span></span>
<dd>
<p>Null-terminated string.</p>
</dd></dl>
<dl>
<dt><span class=nonLinkTerms><span class=parameter>locale</span></span>
<dd>
<p>Locale to use.</p>
</dd></dl>
<div class=LW_CollapsibleArea_Container xmlns="">
<div class=LW_CollapsibleArea_TitleDiv><span class=LW_CollapsibleArea_Title>Return Value</span>
<div class=LW_CollapsibleArea_HrDiv>
<hr class=LW_CollapsibleArea_Hr>
</div>
</div>
<a name=returnValueToggle xmlns="http://www.w3.org/1999/xhtml"></a>
<p xmlns="http://www.w3.org/1999/xhtml">Each of these functions returns the number of characters in <span class=parameter>str</span>, excluding the terminal <strong>NULL</strong>. No return value is reserved to indicate an error, except for <strong>_mbstrlen</strong>, which returns -1 if the string contains an invalid multibyte character.</p>
</div>
<div class=LW_CollapsibleArea_Container xmlns="">
<div class=LW_CollapsibleArea_TitleDiv><span class=LW_CollapsibleArea_Title>Remarks</span>
<div class=LW_CollapsibleArea_HrDiv>
<hr class=LW_CollapsibleArea_Hr>
</div>
</div>
<a name=remarksToggle xmlns="http://www.w3.org/1999/xhtml"></a>
<p xmlns="http://www.w3.org/1999/xhtml">However, <strong>strlen</strong> interprets the string as a single-byte character string, so its return value is always equal to the number of bytes, even if the string contains multibyte characters. <strong>wcslen</strong> is a wide-character version of <strong>strlen</strong>; the argument of <strong>wcslen</strong> is a wide-character string and the count of characters is in wide (two-byte) characters. <strong>wcslen</strong> and <strong>strlen</strong> behave identically otherwise.</p>
<p xmlns="http://www.w3.org/1999/xhtml"><strong>Security Note</strong>&nbsp;&nbsp;&nbsp;&nbsp;These functions incur a potential threat brought about by a buffer overrun problem. Buffer overrun problems are a frequent method of system attack, resulting in an unwarranted elevation of privilege. For more information, see <a href="http://msdn.microsoft.com/en-us/library/ms717795(v=VS.80).aspx"><font color=#1364c4>Avoiding Buffer Overruns</font></a>.</p>
<div class=labelheading xmlns="http://www.w3.org/1999/xhtml"><strong>Generic-Text Routine Mappings</strong></div>
<div class=tableSection xmlns="http://www.w3.org/1999/xhtml">
<table width="100%">
    <tbody>
        <tr>
            <th>TCHAR.H routine </th>
            <th>_UNICODE &amp; _MBCS not defined </th>
            <th>_MBCS defined </th>
            <th>_UNICODE defined </th>
        </tr>
        <tr>
            <td>
            <p><strong>_tcslen</strong></p>
            </td>
            <td>
            <p><strong>strlen</strong></p>
            </td>
            <td>
            <p><strong>strlen</strong></p>
            </td>
            <td>
            <p><strong>wcslen</strong></p>
            </td>
        </tr>
        <tr>
            <td>
            <p><strong>_tcsclen</strong></p>
            </td>
            <td>
            <p><strong>strlen</strong></p>
            </td>
            <td>
            <p><strong>_mbslen</strong></p>
            </td>
            <td>
            <p><strong>wcslen</strong></p>
            </td>
        </tr>
        <tr>
            <td>
            <p><strong>_tcsclen_l</strong></p>
            </td>
            <td>
            <p><strong>strlen_l</strong></p>
            </td>
            <td>
            <p><strong>_mbslen_l</strong></p>
            </td>
            <td>
            <p><strong>wcslen_l</strong></p>
            </td>
        </tr>
    </tbody>
</table>
</div>
<p xmlns="http://www.w3.org/1999/xhtml"><strong>_mbslen</strong>,<strong></strong><strong>_mbslen_l</strong>, <strong>_mbstrlen</strong><strong></strong>and<strong></strong><strong>_mbstrlen_l</strong> return the number of multibyte characters in a multibyte-character string but they do not test for multibyte-character validity. <strong>_mbstrlen</strong><strong></strong>and<strong></strong><strong>_mbstrlen_l</strong> tests for multibyte-character validity and recognizes multibyte-character sequences<span class=linkTerms><a href="http://msdn.microsoft.com/en-us/library/x99tb11d(v=VS.80).aspx"><font color=#1364c4>setlocale, _wsetlocale</font></a></span>. If the string passed to <strong>_mbstrlen</strong> or<strong></strong><strong>_mbstrlen_l</strong> contain an invalid multibyte character for the code page, it returns -1 and sets <strong>errno</strong> to <strong>EILSEQ</strong>.</p>
<p xmlns="http://www.w3.org/1999/xhtml">The output value is affected by the setting of the <strong>LC_CTYPE</strong> category setting of the locale; see <a href="http://msdn.microsoft.com/en-us/library/x99tb11d(v=VS.80).aspx"><font color=#1364c4>setlocale</font></a> for more information. The versions of these functions without the <strong>_l</strong> suffix use the current locale for this locale-dependent behavior; the versions with the <strong>_l</strong> suffix are identical except that they use the locale parameter passed in instead.</p>
</div>
<div class=LW_CollapsibleArea_Container xmlns="">
<div class=LW_CollapsibleArea_TitleDiv><span class=LW_CollapsibleArea_Title>Requirements</span>
<div class=LW_CollapsibleArea_HrDiv>
<hr class=LW_CollapsibleArea_Hr>
</div>
</div>
<a name=requirementsToggle xmlns="http://www.w3.org/1999/xhtml"></a>
<div class=labelheading xmlns="http://www.w3.org/1999/xhtml"><strong></strong></div>
<div class=tableSection xmlns="http://www.w3.org/1999/xhtml">
<table width="100%">
    <tbody>
        <tr>
            <th>Routine </th>
            <th>Required header </th>
            <th>Compatibility </th>
        </tr>
        <tr>
            <td>
            <p><strong>strlen</strong></p>
            </td>
            <td>
            <p>&lt;string.h&gt;</p>
            </td>
            <td>
            <p>ANSI, Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003</p>
            </td>
        </tr>
        <tr>
            <td>
            <p><strong>strlen_l</strong></p>
            </td>
            <td>
            <p>&lt;string.h&gt;</p>
            </td>
            <td>
            <p>Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003</p>
            </td>
        </tr>
        <tr>
            <td>
            <p><strong>wcslen</strong>, <strong>wcslen_l</strong></p>
            </td>
            <td>
            <p>&lt;string.h&gt; or &lt;wchar.h&gt;</p>
            </td>
            <td>
            <p>ANSI, Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003</p>
            </td>
        </tr>
        <tr>
            <td>
            <p><strong>_mbslen</strong>, <strong>_mbslen_l</strong></p>
            </td>
            <td>
            <p>&lt;mbstring.h&gt;</p>
            </td>
            <td>
            <p>Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003</p>
            </td>
        </tr>
        <tr>
            <td>
            <p><strong>_mbstrlen</strong>, <strong>_mbstrlen_l</strong></p>
            </td>
            <td>
            <p>&lt;stdlib.h&gt;</p>
            </td>
            <td>
            <p>Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003</p>
            </td>
        </tr>
    </tbody>
</table>
</div>
<p xmlns="http://www.w3.org/1999/xhtml">For additional compatibility information, see <a href="http://msdn.microsoft.com/en-us/library/sk54f3f5(v=VS.80).aspx"><font color=#1364c4>Compatibility</font></a> in the Introduction.</p>
</div>
<div class=LW_CollapsibleArea_Container xmlns="">
<div class=LW_CollapsibleArea_TitleDiv><span class=LW_CollapsibleArea_Title>Example</span>
<div class=LW_CollapsibleArea_HrDiv>
<hr class=LW_CollapsibleArea_Hr>
</div>
</div>
<a name=codeExampleToggle xmlns="http://www.w3.org/1999/xhtml"></a>
<div class=LW_CodeSnippetContainer xmlns=""><a name=CodeSpippet1></a>
<div class=LW_CodeSnippetContainerCodeCollection>
<div class=LW_CodeSnippetToolBar>
<div class=LW_CodeSnippetToolBarText style="valign: top"><a title="Copy to clipboard." href="javascript:CodeSnippet_CopyCode('CodeSnippetContainerCode1');"><font color=#1364c4 size=2>Copy</font></a></div>
</div>
<div class=LW_CodeSnippetContainerCode id=CodeSnippetContainerCode1>
<div style="COLOR: black">
<pre>// crt_strlen.c
// Determine the length of a string. For the multi-byte character
// example to work correctly, the Japanese language support for
// non-Unicode programs must be enabled by the operating system.
#include &lt;string.h&gt;
#include &lt;locale.h&gt;
int main()
{
char* str1 = "Count.";
wchar_t* wstr1 = L"Count.";
char * mbstr1;
char * locale_string;
// strlen gives the length of single-byte character string
printf("Length of '%s' : %d\n", str1, strlen(str1) );
// wstrlen gives the length of a wide character string
wprintf(L"Length of '%s' : %d\n", wstr1, wcslen(wstr1) );
// A multibyte string: [A] [B] [C] [katakana A] [D] [\0]
// in Code Page 932. For this example to work correctly,
// the Japanese language support must be enabled by the
// operating system.
mbstr1 = "ABC" "\x83\x40" "D";
locale_string = setlocale(LC_CTYPE, "Japanese_Japan");
if (locale_string == NULL)
{
printf("Japanese locale not enabled. Exiting.\n");
exit(1);
}
else
{
printf("Locale set to %s\n", locale_string);
}
// _mbslen will recognize the Japanese multibyte character if the
// current locale used by the operating system is Japanese
printf("Length of '%s' : %d\n", mbstr1, _mbslen(mbstr1) );
// _mbstrlen will recognize the Japanese multibyte character
// since the CRT locale is set to Japanese even if the OS locale
// isnot.
printf("Length of '%s' : %d\n", mbstr1, _mbstrlen(mbstr1) );
printf("Bytes in '%s' : %d\n", mbstr1, strlen(mbstr1) );
}
</pre>
</div>
</div>
</div>
</div>
<h4 class=subHeading xmlns="http://www.w3.org/1999/xhtml">Sample Output</h4>
<pre xmlns="http://www.w3.org/1999/xhtml">Length of 'Count.' : 6
Length of 'Count.' : 6
Length of 'ABCァD' : 5
Length of 'ABCァD' : 5
Bytes in 'ABCァD' : 6</pre>
</div>
<div class=LW_CollapsibleArea_Container xmlns="">
<div class=LW_CollapsibleArea_TitleDiv><span class=LW_CollapsibleArea_Title>.NET Framework Equivalent</span></div>
</div>
</div>
</div>
<img src ="http://www.cppblog.com/fdsajhg/aggbug/125765.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fdsajhg/" target="_blank">傅先生</a> 2010-09-03 11:07 <a href="http://www.cppblog.com/fdsajhg/archive/2010/09/03/125765.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Unicode 字符输出</title><link>http://www.cppblog.com/fdsajhg/archive/2010/09/03/125764.html</link><dc:creator>傅先生</dc:creator><author>傅先生</author><pubDate>Fri, 03 Sep 2010 03:04:00 GMT</pubDate><guid>http://www.cppblog.com/fdsajhg/archive/2010/09/03/125764.html</guid><wfw:comment>http://www.cppblog.com/fdsajhg/comments/125764.html</wfw:comment><comments>http://www.cppblog.com/fdsajhg/archive/2010/09/03/125764.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fdsajhg/comments/commentRss/125764.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fdsajhg/services/trackbacks/125764.html</trackback:ping><description><![CDATA[<div id=content>#deinfe Const_BufLength 200<br><br>wchar_t strzh=new wchar_t[Const_BufLength]; //定义<br>&nbsp;<code class="cpp plain">wprintf(L</code><code class="cpp string"><font color=#0000ff>"Zhong text is: %ls\n"</font></code><code class="cpp plain">, strzh); //使用</code>
<p><br>////////////////////////////////////////////////////////<br>&nbsp;</p>
<p>这几天研究Unicode，勤快些，把看到的东西觉得有用的都copy了下来，文章是拼凑出来的，所以看起来会觉得有些乱 ：）。</p>
<p>&nbsp;</p>
<p>&nbsp;1. wprintf<br>Q : sizeof(wchar_t) = ?<br>A : 随编译器不同。(所以:在需要跨平台的时候尽量不用wchar_t) vc : sizeof(wchar_t) = 2;<br><br>Q: 在vc中，为什么直接使用wprintf(L"测试1234")会没有结果<br>A: 没有设置好locale,这样做<br><br></p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">setlocale(LC_ALL&nbsp;,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">chs</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>wprintf(L</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%s</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,L</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">测试1234</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);</span></div>
<p><br>或者(假设当前活动codepage为chs)<br></p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;scp[</span><span style="COLOR: #000000">16</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;cp&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;GetACP();<br>sprintf(scp,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">.%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,cp);<br>setlocale(&nbsp;LC_ALL,&nbsp;scp&nbsp;);<br>wprintf(L</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">测试1234</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);</span></div>
<p><br>2. wcout<br>一样，不过设定locale,请用std::locale</p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;locale&nbsp;loc(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">chs</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;wcout.imbue(loc);<br>&nbsp;&nbsp;&nbsp;&nbsp;wcout&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;L</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">测试1234</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;endl;<br></span></div>
<p>&nbsp;</p>
<p>这篇文章应该是[<a href="http://blog.vckbase.com/netsin/" target=_blank><u><font color=#0000ff>netsin</font></u></a>]的成果，我勤快，记下来。<br>注：wprintf是C的标准库函数，但wcout不是C++的标准成员，C++中的 L"&#8230;&#8230;" 是宽字符，却未必是unicode字符，这与编译器实现相关。<br>[<a href="http://blog.vckbase.com/smileonce/" target=_blank><u><font color=#0000ff>乾坤一笑</font></u></a>]说：为什么 C/C++ 语言把 L"xx" 定义为由实现决定的呢？这显然是为了 C/C++ 的普适性、可移植性。Bjarne 的观点认为，C++ 的方式是允许程序员使用任何字符集作为串的字符类型。另外，unicode 编码已经发展了若干版本了，是否能永久适合下去也不得而知。有关 unicode 的详细论述以及和其它字符集的比较，我推荐你看《无废话xml》。</p>
<p><br>以下两段代码的执行环境是 windows xp professional 英文版，编译器是 VS2005RTM。</p>
<p>// C<br>#include &lt;stdio.h&gt;<br>#include &lt;locale.h&gt;<br>int main( void )<br>{<br>&nbsp;&nbsp;&nbsp; setlocale( LC_ALL, "chs" );<br>&nbsp;&nbsp;&nbsp; //setlocale( LC_ALL, "Chinese-simplified" );<br>&nbsp;&nbsp;&nbsp; //setlocale( LC_ALL, "ZHI" );<br>&nbsp;&nbsp;&nbsp; //setlocale( LC_ALL, ".936" );<br>&nbsp;&nbsp;&nbsp; wprintf( L"中国" );</p>
<p>&nbsp;&nbsp;&nbsp; return 0;<br>}</p>
<p>// C++<br>#include &lt;iostream&gt;<br>#include &lt;locale&gt;<br>using namespace std;<br>int main( void )<br>{<br>&nbsp;&nbsp;&nbsp; locale loc( "chs" );<br>&nbsp;&nbsp;&nbsp; //locale loc( "Chinese-simplified" );<br>&nbsp;&nbsp;&nbsp; //locale loc( "ZHI" );<br>&nbsp;&nbsp;&nbsp; //locale loc( ".936" );<br>&nbsp;&nbsp;&nbsp; wcout.imbue( loc );<br>&nbsp;&nbsp;&nbsp; std::wcout &lt;&lt; L"中国" &lt;&lt; endl;</p>
<p>&nbsp;&nbsp;&nbsp; return 0;<br>}</p>
<p>说明：别混合使用 setlocale 和 std::locale 。<br><br>------------------------- <a href="http://blog.vckbase.com/bruceteen/archive/2005/11/15/14924.html#20060705"><u><font color=#0000ff>2006-07-05 记</font></u></a><a name=20060705></a> -------------------------</p>
<p>&nbsp;"VC知识库"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 编码为：56 43 D6 AA CA B6 BF E2 00&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // ANSI编码<br>L"VC知识库" 在VC++&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 中编码为：56 00 43 00 E5 77 C6 8B 93 5E 00 00&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // (windows口中的unicode)编码<br>L"VC知识库" 在GCC（Dev-CPP4990） 中编码为：56 00 43 00 D6 00 AA 00 CA 00 B6 00 BF 00 E2 00 00 00 // 只是将ANSI编码简单的加0<br>L"VC知识库" 在GCC（Dev-CPP4992） 中编译失败，报 Illegal byte sequence</p>
<p>L"VC知识库" 在 Dev-CPP4992 中解决步骤为：<br>a. 将文件保存为 utf-8 编码&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // utf-8 是unicode的其中一种，但和(windows口中的unicode)不一样<br>b. 去掉BOM头：用二进制编辑器（比如VC）去掉刚才utf-8文件的前三个字节 // Linux/UNIX并不使用BOM<br>c. 使用 gcc/g++ 编译运行</p>
<p>经过以上解决步骤，在 dev-cpp4992 中<br>&nbsp;"VC知识库" 编码为： 56 43 E7 9F A5 E8 AF 86 E5 BA 93 00 // utf-8编码，注意不再是ANSI编码了，因此用 printf/cout 将输出乱码<br>L"VC知识库" 编码为： 56 00 43 00 E5 77 C6 8B 93 5E 00 00 // (windows口中的unicode)编码</p>
<p>补充：在mingw32中使用wcout和wstring需要加一些宏，比如<br>#define _GLIBCXX_USE_WCHAR_T 1<br>#include &lt;iostream&gt;<br>int main( void )<br>{<br>&nbsp;&nbsp;&nbsp; std::wcout &lt;&lt; 1 &lt;&lt; std::endl;<br>}<br>可以编译通过，但无法Link通过，在网上google了一下，stlport说mingw32有问题，mingw32说是M$的c runtime有问题。</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<div class=post>
<div class=posthead>
<h2><a class=singleposttitle id=viewpost1_TitleUrl href="http://www.cppblog.com/sandy/archive/2006/06/21/8779.html"><u><font color=#0000ff>printf&nbsp;&nbsp;、wprintf 在console下的unicode 输出</font></u></a></h2>
<!--
<rdf:rdf xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
<rdf:description
rdf:about="http://www.cppblog.com/sandy/archive/2006/06/21/8779.html"
dc:identifier="http://www.cppblog.com/sandy/archive/2006/06/21/8779.html"
dc:title="深入 printf / wprintf / console下的unicode output"
trackback:ping="http://www.cppblog.com/sandy/services/trackbacks/8779.aspx" />
</rdf:rdf>
--></div>
<div class=postbody>1. printf 只能提供ANSI/MB 的输出，不支持输出unicode stream.<br>例如:<br></div>
<div class=postbody>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">wchar_t&nbsp;test[]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">L</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">测试1234</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br>printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%s</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,test);</span></div>
</div>
<div class=postbody>是不会正确输出的<br><br><br>2.wprintf 同样不会提供unicode output,<br>&nbsp;&nbsp; 但是他会把wchar_t的string转为locale的SB/MB字符编码，然后输出<br>例如：<br></div>
<div class=postbody>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">wchar_t&nbsp;test[]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;L</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">测试Test</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br>wprintf(L</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%s</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,test);</span></div>
</div>
<div class=postbody>会输出??1234之类的字符串，或者不输出任何结果<br>因为wprintf没有办法把L"测试Test"转为默认的ANSI,需要设置locale<br></div>
<div class=postbody>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">setlocale(LC_ALL,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">chs</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>wchar_t&nbsp;test[]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;L</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">测试Test</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br>wprintf(L</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%s</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,test);</span></div>
</div>
<div class=postbody>会有正确的输出<br>等同于printf("%ls",test);<br><br>综上:&nbsp; <font color=#ff3300><strong>CRT I/O functions do not provide Unicode output.<br><br></strong><font color=#000000>3. Window console自从NT4就是一个真正的unicode console<br>不过输出unicode string,只有使用Windows API, WriteConsoleW<br>例如：<br></div>
<div class=postbody>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">wchar_t&nbsp;test[]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;L</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">测试1234</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br>DWORD&nbsp;ws;<br>WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE),test,wcslen(test),</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">ws,NULL);</span></div>
</div>
<div class=postbody>可以正确的输出而不需要设置locale,因为是真正的unicode的输出，跟codepage无关<br><br>4. 如何实现跨平台的console output<br>&nbsp;&nbsp;&nbsp; 不要使用wchar_t和wprintf,因为这些都依赖于编译器.<br>&nbsp;&nbsp;&nbsp;&nbsp; ICU是IBM的一个成熟的跨平台支持unicode的libary,推荐使用<br><br>以下是ICU的uprintf实现<br><br></div>
<div class=postbody>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;uprintf(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;UnicodeString&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">str)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">buf&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;int32_t&nbsp;len&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;str.length();<br>&nbsp;&nbsp;&nbsp;&nbsp;int32_t&nbsp;bufLen&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;len&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">16</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;int32_t&nbsp;actualLen;<br>&nbsp;&nbsp;&nbsp;&nbsp;buf&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">[bufLen&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];<br>&nbsp;&nbsp;&nbsp;&nbsp;actualLen&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;str.extract(</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,&nbsp;len,&nbsp;buf</span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">,&nbsp;bufLen</span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000">);&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;Default&nbsp;codepage&nbsp;conversion</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;buf[actualLen]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%s</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,&nbsp;buf);<br>&nbsp;&nbsp;&nbsp;&nbsp;delete&nbsp;buf;<br>}</span></div>
</div>
<div class=postbody>它也是先把Unicode string转化为本地的codepage,然后printf，虽然也不是unicode output,但是跨平台，大多数情况会工作得很好。<br></div>
<div class=postbody>后记：</div>
<div class=postbody>mbstowcs(wchar_t *<span class=parameter>wcstr</span>,&nbsp; const char *<span class=parameter>mbstr</span>, size_t <span class=parameter>count</span> )等函数第三个参数</div>
<div class=postbody>count：&nbsp;&nbsp; The maximum number of multibyte characters to convert.</div>
<div class=postbody>指待转换的多字节字符串相对于目前活动locale的字符个数加一。</div>
<div class=postbody>比如：字符串"abc赵123"对于C locale而言count是strlen("abc赵123"),即8+1。</div>
<div class=postbody>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 而对于chinese-simplified.936而言count就是7+1。</div>
<div class=postbody>count的计算必须和mbstowcs在同一个locale下。</div>
</font></font></div>
</div>
<p class=otherinfo>阅读（5467） | 评论（1）| <a onclick="window.clipboardData.setData('text','http://blog.pfan.cn/aurora/18802.html');alert('链接复制成功');" href="javascript:;"><u><font color=#800080>复制链接</font></u></a></p>
<img src ="http://www.cppblog.com/fdsajhg/aggbug/125764.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fdsajhg/" target="_blank">傅先生</a> 2010-09-03 11:04 <a href="http://www.cppblog.com/fdsajhg/archive/2010/09/03/125764.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Unicode 与 Utf-8 应用实例</title><link>http://www.cppblog.com/fdsajhg/archive/2010/09/03/125761.html</link><dc:creator>傅先生</dc:creator><author>傅先生</author><pubDate>Fri, 03 Sep 2010 02:55:00 GMT</pubDate><guid>http://www.cppblog.com/fdsajhg/archive/2010/09/03/125761.html</guid><wfw:comment>http://www.cppblog.com/fdsajhg/comments/125761.html</wfw:comment><comments>http://www.cppblog.com/fdsajhg/archive/2010/09/03/125761.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fdsajhg/comments/commentRss/125761.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fdsajhg/services/trackbacks/125761.html</trackback:ping><description><![CDATA[<code><font style="FONT: 10pt Comic Sans MS; COLOR: #008000">//&nbsp;UTF_8.cpp&nbsp;:&nbsp;Defines&nbsp;the&nbsp;entry&nbsp;point&nbsp;for&nbsp;the&nbsp;console&nbsp;application.<br>//<br><br></font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">#include&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #808080">"stdafx.h"<br></font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">#include&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">&lt;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">stdio</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">.</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">h</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">&gt;<br></font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">#include&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">&lt;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">string</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">.</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">h</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">&gt;<br></font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">#include&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">&lt;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">locale</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">.</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">h</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">&gt;<br><br><br></font><font style="FONT: 10pt Comic Sans MS; COLOR: #008000">//&nbsp;把UTF-8转换成Unicode<br></font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">static&nbsp;void&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">UTF_8ToUnicode</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">(</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">wchar_t</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">*&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">pOut</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">,</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">char&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">*</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">pText</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">char</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">*&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">uchar&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">=&nbsp;(</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">char&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">*)</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">pOut</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">uchar</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">[</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">1</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">]&nbsp;=&nbsp;((</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">pText</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">[</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">]&nbsp;&amp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0x0F</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">)&nbsp;&lt;&lt;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">4</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">)&nbsp;+&nbsp;((</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">pText</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">[</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">1</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">]&nbsp;&gt;&gt;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">2</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">)&nbsp;&amp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0x0F</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">uchar</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">[</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">]&nbsp;=&nbsp;((</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">pText</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">[</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">1</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">]&nbsp;&amp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0x03</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">)&nbsp;&lt;&lt;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">6</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">)&nbsp;+&nbsp;(</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">pText</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">[</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">2</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">]&nbsp;&amp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0x3F</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">);<br>}<br><br><br></font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">void&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">UTF8_UNICODE</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">(</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">char&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">*</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">lpIn</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">,&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">int&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">nLen</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">,&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">wchar_t&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">*</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">lpOut</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">int&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">i&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">=&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">int&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">j&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">=&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">while</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">(&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">i&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">&lt;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">nLen&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">wchar_t&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">unicode&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">=&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">if</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">(&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">lpIn</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">[</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">i</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">]&nbsp;&gt;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*((</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">char&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">*)&amp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">unicode</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">)&nbsp;=&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">lpIn</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">[</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">i</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">++];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">else&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">UTF_8ToUnicode</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">(&amp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">unicode</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">,&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">lpIn&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">+&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">i</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">i&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">+=&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">3</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">lpOut</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">[</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">j</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">++]&nbsp;=&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">unicode</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">lpOut</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">[</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">j</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">]&nbsp;=&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">L</font><font style="FONT: 10pt Fixedsys; COLOR: #808080">'\0'</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">;<br>}<br><br></font><font style="FONT: 10pt Comic Sans MS; COLOR: #008000">///////////------------&nbsp;Unicode转UTF-8，字节&nbsp;<br></font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">int&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">cUxU8</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">(</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">char</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">*&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">pOut</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">,</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">wchar_t</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">*&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">pText</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">){&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">int&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">ret&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">=&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">unsigned&nbsp;char</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">*&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">pchar&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">=&nbsp;(</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">unsigned&nbsp;char&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">*)</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">pText</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">if</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">(</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">pText</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">[</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">]&lt;=</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0x7f</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">){&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Comic Sans MS; COLOR: #008000">//&nbsp;ASCII&nbsp;&nbsp;0x00&nbsp;~&nbsp;0x7f&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">pOut</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">[</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">]&nbsp;=&nbsp;(</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">char</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">)</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">pchar</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">[</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">];&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;}</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">else&nbsp;if</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">(</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">pText</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">[</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">]&lt;=</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0x7ff</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">){&nbsp;&nbsp;</font><font style="FONT: 10pt Comic Sans MS; COLOR: #008000">//&nbsp;0x080&nbsp;~&nbsp;0x7ff&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">pOut</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">[</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">]&nbsp;=&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0xc0</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">|(</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">pchar</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">[</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">1</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">]&lt;&lt;</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">2</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">)|(</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">pchar</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">[</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">]&gt;&gt;</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">6</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">pOut</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">[</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">1</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">]&nbsp;=&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0x80</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">|(</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">pchar</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">[</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">]&amp;</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0x3f</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">ret&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">=&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">1</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;}</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">else</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Comic Sans MS; COLOR: #008000">//&nbsp;0x0800&nbsp;~&nbsp;0xFFFF&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">pOut</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">[</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">]&nbsp;=&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0xe0</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">|(</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">pchar</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">[</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">1</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">]&gt;&gt;</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">4</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">pOut</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">[</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">1</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">]&nbsp;=&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0x80</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">|((</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">pchar</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">[</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">1</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">]&amp;</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0x0f</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">)&lt;&lt;</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">2</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">)|(</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">pchar</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">[</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">]&gt;&gt;</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">6</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">pOut</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">[</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">2</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">]&nbsp;=&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0x80</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">|(</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">pchar</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">[</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">]&amp;</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0x3f</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">ret&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">=&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">2</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">return&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">ret</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">;&nbsp;<br>}&nbsp;<br><br><br></font><font style="FONT: 10pt Comic Sans MS; COLOR: #008000">//&nbsp;------------&nbsp;Unicode转UTF-8，字符串&nbsp;----------------&nbsp;<br></font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">int&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">sUxU8</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">(</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">char</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">*&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">pOut</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">,</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">wchar_t</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">*&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">pText</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">,</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">int&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">Len</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">){&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">int&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">i</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">,</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">j</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">for</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">(</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">i</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">=</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">,</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">j</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">=</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">i</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">&lt;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">Len</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">i</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">++,</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">j</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">++){&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">j</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">+=</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">cUxU8</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">(&amp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">pOut</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">[</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">j</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">],&amp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">pText</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">[</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">i</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">]);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">return&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">j</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">;&nbsp;<br>}&nbsp;<br><br><br><br></font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">int&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">main</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">(</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">int&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">argc</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">,&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">char</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">*&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">argv</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">[])<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">wchar_t&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">*&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">p</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">=&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">L</font><font style="FONT: 10pt Fixedsys; COLOR: #808080">"111222ffdd你你你好你你你"</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">;&nbsp;&nbsp;</font><font style="FONT: 10pt Comic Sans MS; COLOR: #008000">//测试代码<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;--输出Unicode其编码<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">int&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">plen&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">=&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">wcslen</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">(</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">p</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">);<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">printf</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">(</font><font style="FONT: 10pt Fixedsys; COLOR: #808080">"开始unicode&nbsp;P&nbsp;:"</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">for</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">(</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">int&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">i</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">=</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">i</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">&lt;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">plen</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">i</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">++)</font><font style="FONT: 10pt Comic Sans MS; COLOR: #008000">//<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">printf</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">(</font><font style="FONT: 10pt Fixedsys; COLOR: #808080">"%X&nbsp;"</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">,</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">p</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">[</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">i</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">]);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">printf</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">(</font><font style="FONT: 10pt Fixedsys; COLOR: #808080">"\n"</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">);&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">printf</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">(</font><font style="FONT: 10pt Fixedsys; COLOR: #808080">"\n"</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">);<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Comic Sans MS; COLOR: #008000">//开始从unicode~~~UTF-8&nbsp;转换&nbsp;&nbsp;--并输出其编码<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">char&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">pOut</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">[</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">500</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">];<br><br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">sUxU8</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">(</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">pOut</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">,</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">p</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">,</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">plen</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">*</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">2</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">printf</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">(</font><font style="FONT: 10pt Fixedsys; COLOR: #808080">"开始UTF-8&nbsp;pOut&nbsp;:"</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">for</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">(</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">i</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">=</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">i</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">&lt;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">plen</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">*</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">3</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">i</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">++)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">printf</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">(</font><font style="FONT: 10pt Fixedsys; COLOR: #808080">"%X&nbsp;"</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">,</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">pOut</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">[</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">i</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">]);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">printf</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">(</font><font style="FONT: 10pt Fixedsys; COLOR: #808080">"\n"</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">);&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">printf</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">(</font><font style="FONT: 10pt Fixedsys; COLOR: #808080">"\n"</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">);<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Comic Sans MS; COLOR: #008000">//开始从UTF-8~~~unicode&nbsp;还原&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--并输出其编码<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">wchar_t&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">wtest</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">[</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">500</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">];<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">UTF8_UNICODE</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">(</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">pOut</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">,&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">plen</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">*</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">2</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">,&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">wtest</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">printf</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">(</font><font style="FONT: 10pt Fixedsys; COLOR: #808080">"开始Unicode&nbsp;还原:"</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">for</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">(</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">i</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">=</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">i</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">&lt;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">plen</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">*</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">3</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">i</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">++)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">printf</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">(</font><font style="FONT: 10pt Fixedsys; COLOR: #808080">"%X&nbsp;"</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">,</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">wtest</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">[</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">i</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">]);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">printf</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">(</font><font style="FONT: 10pt Fixedsys; COLOR: #808080">"\n"</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">);&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">printf</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">(</font><font style="FONT: 10pt Fixedsys; COLOR: #808080">"\n"</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">);<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Comic Sans MS; COLOR: #008000">///输出&nbsp;Unicode字符<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">setlocale</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">(</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">LC_ALL</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">,</font><font style="FONT: 10pt Fixedsys; COLOR: #808080">"chs"</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">wchar_t&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">test</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">[]&nbsp;=&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">L</font><font style="FONT: 10pt Fixedsys; COLOR: #808080">"测试Test"</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">wprintf</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">(</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">L</font><font style="FONT: 10pt Fixedsys; COLOR: #808080">"内容还原&nbsp;&nbsp;：&nbsp;%s&nbsp;\n"</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">,</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">wtest</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">);<br>&nbsp;&nbsp;&nbsp;&nbsp;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #000000">printf</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">(</font><font style="FONT: 10pt Fixedsys; COLOR: #808080">"Hello&nbsp;World!\n"</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #0000ff">return&nbsp;</font><font style="FONT: 10pt Fixedsys; COLOR: #800080">0</font><font style="FONT: 10pt Fixedsys; COLOR: #000080">;<br>}<br><br><br><br><br><br><br><br><br><br></font></code>
<img src ="http://www.cppblog.com/fdsajhg/aggbug/125761.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fdsajhg/" target="_blank">傅先生</a> 2010-09-03 10:55 <a href="http://www.cppblog.com/fdsajhg/archive/2010/09/03/125761.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>VC用ADO访问数据库全攻略</title><link>http://www.cppblog.com/fdsajhg/archive/2010/08/14/123450.html</link><dc:creator>傅先生</dc:creator><author>傅先生</author><pubDate>Sat, 14 Aug 2010 10:49:00 GMT</pubDate><guid>http://www.cppblog.com/fdsajhg/archive/2010/08/14/123450.html</guid><wfw:comment>http://www.cppblog.com/fdsajhg/comments/123450.html</wfw:comment><comments>http://www.cppblog.com/fdsajhg/archive/2010/08/14/123450.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fdsajhg/comments/commentRss/123450.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fdsajhg/services/trackbacks/123450.html</trackback:ping><description><![CDATA[<p>VC用ADO访问数据库全攻略 收藏 <br>(由于vchelp网站被封，故将此文章转移至此，愿它能为光大VC编程者一个抛砖引玉的作用)</p>
<p>一、ADO概述<br>ADO是Microsoft为最新和最强大的数据访问范例 OLE DB 而设计的，是一个便于使用的应用程序层接口。ADO 使您能够编写应用程序以通过 OLE. DB 提供者访问和操作数据库服务器中的数据。ADO 最主要的优点是易于使用、速度快、内存支出少和磁盘遗迹小。ADO 在关键的应用方案中使用最少的网络流量，并且在前端和数据源之间使用最少的层数，所有这些都是为了提供轻量、高性能的接口。之所以称为 ADO，是用了一个比较熟悉的暗喻，OLE 自动化接口。</p>
<p>OLE DB是一组&#8221;组件对象模型&#8221;(COM) 接口，是新的数据库低层接口，它封装了ODBC的功能，并以统一的方式访问存储在不同信息源中的数据。OLE DB是Microsoft UDA(Universal Data Access)策略的技术基础。OLE DB 为任何数据源提供了高性能的访问，这些数据源包括关系和非关系数据库、电子邮件和文件系统、文本和图形、自定义业务对象等等。也就是说，OLE DB 并不局限于 ISAM、Jet 甚至关系数据源，它能够处理任何类型的数据，而不考虑它们的格式和存储方法。在实际应用中，这种多样性意味着可以访问驻留在 Excel 电子数据表、文本文件、电子邮件/目录服务甚至邮件服务器，诸如 Microsoft Exchange 中的数据。但是，OLE DB 应用程序编程接口的目的是为各种应用程序提供最佳的功能，它并不符合简单化的要求。您需要的API 应该是一座连接应用程序和OLE DB 的桥梁，这就是 ActiveX Data Objects (ADO)。 </p>
<p>二、在VC中使用ADO(开发步骤好下：) </p>
<p>1、引入ADO库文件 </p>
<p>使用ADO前必须在工程的stdafx.h头文件里用直接引入符号#import引入ADO库文件,以使编译器能正确编译。代码如下所示： </p>
<p>用#import引入ADO库文件 </p>
<p>#import "c:\program files\common files\system\ado\msado15.dll"no_namespaces rename("EOF" adoEOF") </p>
<p>这行语句声明在工程中使用ADO，但不使用ADO的名字空间，并且为了避免常数冲突，将常数EOF改名为adoEOF。现在不需添加另外的头文件，就可以使用ADO接口了。 </p>
<p>2、初始化OLE/COM库环境<br>必须注意的是，ADO库是一组COM动态库，这意味应用程序在调用ADO前，必须初始化OLE/COM库环境。在MFC应用程序里，一个比较好的方法是在应用程序主类的InitInstance成员函数里初始化OLE/COM库环境。 </p>
<p>BOOL CMyAdoTestApp：：InitInstance()<br>{<br>if(!AfxOleInit())//这就是初始化COM库<br>{<br>AfxMessageBox(&#8220;OLE初始化出错!&#8221;);<br>return FALSE;<br>} </p>
<p>&#8230;&#8230; </p>
<p>} </p>
<p>3、ADO接口简介 </p>
<p>ADO库包含三个基本接口:_ConnectionPtr接口、_CommandPtr接口和_RecordsetPtr接口。<br>_ConnectionPtr接口返回一个记录集或一个空指针。通常使用它来创建一个数据连接或执行一条不返回任何结果的SQL语句，如一个存储过程。使用_ConnectionPtr接口返回一个记录集不是一个好的使用方法。对于要返回记录的操作通常用_RecordserPtr来实现。而用_ConnectionPtr操作时要想得到记录条数得遍历所有记录，而用_RecordserPtr时不需要。 </p>
<p>_CommandPtr接口返回一个记录集。它提供了一种简单的方法来执行返回记录集的存储过程和SQL语句。在使用_CommandPtr接口时，你可以利用全局_ConnectionPtr接口，也可以在_CommandPtr接口里直接使用连接串。如果你只执行一次或几次数据访问操作，后者是比较好的选择。但如果你要频繁访问数据库，并要返回很多记录集，那么，你应该使用全局_ConnectionPtr接口创建一个数据连接，然后使用_CommandPtr接口执行存储过程和SQL语句。 </p>
<p>_RecordsetPtr是一个记录集对象。与以上两种对象相比，它对记录集提供了更多的控制功能，如记录锁定，游标控制等。同_CommandPtr接口一样，它不一定要使用一个已经创建的数据连接，可以用一个连接串代替连接指针赋给_RecordsetPtr的connection成员变量，让它自己创建数据连接。如果你要使用多个记录集，最好的方法是同Command对象一样使用已经创建了数据连接的全局_ConnectionPtr接口<br>，然后使用_RecordsetPtr执行存储过程和SQL语句。　 </p>
<p>4、使用_ConnectionPtr接口<br>_ConnectionPtr主要是一个连接接口，取得与数据库的连接。它的连接字符串可以是自己直接写，也可以指向一个ODBC　DSN。。　<br>&nbsp; _ConnectionPtr pConn;<br>if (FAILED(pConn.CreateInstance("ADODB.Connection")))<br>{<br>AfxMessageBox("Create Instance failed!");<br>return;<br>}</p>
<p><br>CString strSRC;<br>strSRC="Driver=SQL Server;Server=";<br>strSRC+="suppersoft";<br>strSRC+=";Database=";<br>strSRC+="mydb";<br>strSRC+=";UID=SA;PWD=";</p>
<p>CString strSQL = "Insert into student(no,name,sex,address) values(3,'aaa','male','beijing')";</p>
<p>_variant_t varSRC(strSRC);<br>_variant_t varSQL(strSQL);<br>_bstr_t bstrSRC(strSRC);</p>
<p>if (FAILED(pConn-&gt;Open(bstrSRC,"","",-1)))<br>{<br>AfxMessageBox("Can not open Database!");<br>pConn.Release();<br>return;<br>}</p>
<p>COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR);</p>
<p>pConn-&gt;Execute(_bstr_t(strSQL),&amp;vtOptional,-1);</p>
<p>pConn.Release();</p>
<p>AfxMessageBox("ok!");</p>
<p>5、使用_RecordsetPtr接口(以连接SQL Server为例)<br>_RecordsetPtr pPtr;<br>if (FAILED(pPtr.CreateInstance("ADODB.Recordset")))<br>{<br>AfxMessageBox("Create Instance failed!");<br>return FALSE;<br>}</p>
<p>CString strSRC;<br>strSRC="Driver=SQL Server;Server=";<br>strSRC+="210.46.141.145";<br>strSRC+=";Database=";<br>strSRC+="mydb";<br>strSRC+=";UID=sa;PWD=";<br>strSRC+="sa";</p>
<p>CString strSQL = "select id,name,gender,address from personal";</p>
<p>_variant_t varSRC(strSRC);<br>_variant_t varSQL(strSQL);</p>
<p>if(FAILED(pPtr-&gt;Open(varSQL,varSRC,adOpenStatic,adLockOptimistic,adCmdText)))<br>{<br>AfxMessageBox("Open table failed!");<br>pPtr.Release();<br>return FALSE;<br>}</p>
<p>while(!pPtr-&gt;GetadoEOF())<br>{<br>_variant_t varNo;<br>_variant_t varName;<br>_variant_t varSex;<br>_variant_t varAddress;</p>
<p>varNo = pPtr-&gt;GetCollect ("id");<br>varName = pPtr-&gt;GetCollect ("name");<br>varSex = pPtr-&gt;GetCollect ("gender");<br>varAddress = pPtr-&gt;GetCollect ("address");</p>
<p>CString strNo =(char *)_bstr_t(varNo);<br>CString strName =(char *)_bstr_t(varName);<br>CString strSex =(char *)_bstr_t(varSex);<br>CString strAddress =(char *)_bstr_t(varAddress);</p>
<p>strNo.TrimRight();<br>strName.TrimRight();<br>strSex.TrimRight();<br>strAddress.TrimRight();</p>
<p>int nCount = m_list.GetItemCount();<br>int nItem = m_list.InsertItem (nCount,_T(""));<br>m_list.SetItemText (nItem,0,strNo);<br>m_list.SetItemText (nItem,1,strName);<br>m_list.SetItemText (nItem,2,strSex);<br>m_list.SetItemText (nItem,3,strAddress);</p>
<p>pPtr-&gt;MoveNext();<br>}</p>
<p>pPtr-&gt;Close();<br>pPtr.Release(); </p>
<p>6、使用_CommandPtr接口<br>_CommandPtr接口返回一个Recordset对象，并且提供了更多的记录集控制功能，以下代码示例了使用_CommandPtr接口的方法： </p>
<p>代码11:使用_CommandPtr接口获取数据<br>_CommandPtr pCommand;<br>_RecordsetPtr pRs;<br>pCommand.CreateInstance(__uuidof(Command));<br>pCommand-&gt;ActiveConnection=pConn;<br>pCommand-&gt;CommandText="select * from student";<br>pCommand-&gt;CommandType=adCmdText;<br>pCommand-&gt;Parameters-&gt;Refresh();<br>pRs=pCommand-&gt;Execute(NULL,NULL,adCmdUnknown);<br>_variant_t varValue = pRs-&gt;GetCollect("name");<br>Cstring strValue=(char*)_bstr_t(varValue); </p>
<p>6、关于数据类型转换由于COM对象是跨平台的，它使用了一种通用的方法来处理各种类型的数据，<br>因此Cstring 类和COM对象是不兼容的，我们需要一组API来转换COM对象和C++类型的数据。_vatiant_t和_bstr_t就是这样两种对象。它们提供了通用的方法转换COM对象和C++类型的数据。</p>
<p><br>发表于 @ 2008年08月28日　08:47:00 | 评论( 3 ) | 编辑| 举报| </p>
<p><br>本文来自CSDN博客，转载请标明出处：<a href="http://blog.csdn.net/xsc2001/archive/2008/08/28/2841301.aspx">http://blog.csdn.net/xsc2001/archive/2008/08/28/2841301.aspx</a></p>
<img src ="http://www.cppblog.com/fdsajhg/aggbug/123450.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fdsajhg/" target="_blank">傅先生</a> 2010-08-14 18:49 <a href="http://www.cppblog.com/fdsajhg/archive/2010/08/14/123450.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>