zackary 发表于 2007-5-15 16:47:00

该死的Unicode

<p>请教如何将一 ads_real类型实数转化为ACHAR*类型,以便在图形中输出。</p><p>例如,我要在图形中插入“壁厚Xmm”说明语句。其中X为ads_real型实数的值。请教高手如何实现。</p><p>说明:俺的程序没有基于MFC,CString类不能用。</p>

guojiang 发表于 2007-5-15 17:33:00

<p>acutprintf(_T("壁厚%lfmm"),X);</p><p></p><p></p><p>是不是?</p>

zackary 发表于 2007-5-15 18:25:00

<p>&nbsp;多谢,如果要acedAlert()函数,或者用acdbText的setTextString函数呢</p>

guojiang 发表于 2007-5-16 01:55:00

<p class="signature">extern "C" int</p><p class="signature">acedAlert(</p><p class="signatureInEnd">const ACHAR * str);</p><table class="argumentList"><tbody><tr valign="baseline"><td class="argumentName">str</td><td class="argumentDescription">Message to display (maximum of 132 characters with the 133rd slot reserved as the end-of-string character)</td></tr></tbody></table><p class="signature">__________________________________________________________</p><p class="signature">Acad::ErrorStatus</p><p class="signature">setTextString(</p><p class="signatureInEnd">const ACHAR* unnamed);</p><table class="argumentList"><tbody><tr valign="baseline"><td class="argumentName">unnamed</td><td class="argumentDescription">Input null-terminated text string (256 byte max)</td></tr></tbody></table><p>__________________________________________________________</p><p>两个应该和上面那个一样。只要加个_T("")就行了吧。</p><p></p><p>我也是菜鸟,自己想的,呵呵。</p>

zackary 发表于 2007-5-16 10:29:00

<div class="tit">“MultiByteToWideChar和WideCharToMultiByte用法详解”</div><div class="date">2006-12-27 14:50</div><table style="TABLE-LAYOUT: fixed;"><tbody><tr><td><div class="cnt"><div class="postTitle"><a href="http://blog.csdn.net/norains/archive/2006/12/25/1461174.aspx"><font color="#800080">&nbsp;MultiByteToWideChar和WideCharToMultiByte用法详解</font></a>&nbsp;</div><div class="postTitle"></div><div class="postText">//========================================================================<br/>//TITLE:<br/>//&nbsp;&nbsp;&nbsp;&nbsp;MultiByteToWideChar和WideCharToMultiByte用法详解<br/>//AUTHOR:<br/>//&nbsp;&nbsp;&nbsp;&nbsp;norains<br/>//DATE:<br/>//&nbsp;&nbsp;&nbsp;&nbsp;Thursday&nbsp;&nbsp;25-December&nbsp;-2006<br/>//Environment:<br/>//&nbsp;&nbsp;EVC4.0&nbsp;+&nbsp;Standard&nbsp;SDK<br/>//========================================================================<br/>&nbsp;&nbsp;在本文开始之处,先简要地说一下何为短字符和宽字符.<br/>&nbsp;&nbsp;所谓的短字符,就是用8bit来表示的字符,典型的应用是ASCII码.而宽字符,顾名思义,就是用16bit表示的字符,典型的有UNICODE.关于windows下的ASCII和UNICODE的更多信息,可以参考这两本经典著作:《windows&nbsp;程序设计》,《windows&nbsp;核心编程》.这两本书关于这两种字符都有比较详细的解说.<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;宽字符转换为多个短字符是一个难点,不过我们只要掌握到其中的要领,便可如鱼得水.<br/>&nbsp;&nbsp;好吧,那就让我们开始吧.<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;这个是我们需要转化的宽字符串:&nbsp;&nbsp;<br/>&nbsp;&nbsp;wchar_t&nbsp;wText&nbsp;=&nbsp;{L"宽字符转换实例!OK!"};<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;我们需要知道转化后的短字符需要多少个数组空间.虽然在这个里程里面,我们可以直接定义一个20*2短字符的数组,并且事实上将运行得非常轻松愉快.但假如宽字符串更多,达到上千个乃至上万个,我们将会发现其中浪费的内存将会越来越多.所以以宽字符的个数的两倍作为短字符数组下标的声明绝对不是一个好主意.<br/>&nbsp;&nbsp;所幸,我们能够确知所需要的数组空间.<br/>&nbsp;&nbsp;我们只需要将MultiByteToWideChar()的第四个形参设为-1,即可返回所需的短字符数组空间的个数:<br/>&nbsp;&nbsp;DWORD&nbsp;dwNum&nbsp;=&nbsp;MultiByteToWideChar&nbsp;(CP_ACP,&nbsp;0,&nbsp;wText,&nbsp;-1,&nbsp;NULL,&nbsp;0);<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;接下来,我们只需要分配响应的数组空间:<br/>&nbsp;&nbsp;char&nbsp;*psText;<br/>&nbsp;&nbsp;psText&nbsp;=&nbsp;new&nbsp;char;<br/>&nbsp;&nbsp;if(!psText)<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;delete&nbsp;[]psText;<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;接着,我们就可以着手进行转换了.在这里以转换成ASCII码做为例子:<br/>&nbsp;&nbsp;MultiByteToWideChar&nbsp;(CP_ACP,&nbsp;0,&nbsp;psText,&nbsp;-1,&nbsp;wText,&nbsp;dwSize);<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;最后,使用完毕当然要记得释放占用的内存:<br/>&nbsp;&nbsp;delete&nbsp;[]psText;<br/>&nbsp;&nbsp;<br/>&nbsp;<br/>&nbsp;&nbsp;同理,多个短字符转为宽字符的代码如下:&nbsp;&nbsp;<br/>&nbsp;&nbsp;char&nbsp;sText&nbsp;=&nbsp;{L"宽字符转换实例!OK!"};<br/>&nbsp;&nbsp;DWORD&nbsp;dwNum&nbsp;=&nbsp;WideCharToMultiByte&nbsp;(CP_ACP,&nbsp;0,&nbsp;sText,&nbsp;-1,&nbsp;NULL,&nbsp;0);<br/>&nbsp;&nbsp;wchar_t&nbsp;*pwText;<br/>&nbsp;&nbsp;pwText&nbsp;=&nbsp;new&nbsp;wchar_t;<br/>&nbsp;&nbsp;if(!psText)<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;delete&nbsp;[]psText;<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;WideCharToMultiByte&nbsp;(CP_ACP,&nbsp;0,&nbsp;pwText,&nbsp;-1,&nbsp;sText,&nbsp;dwSize);<br/>&nbsp;&nbsp;delete&nbsp;[]pwText;<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;如果之前我们已经分配好空间,并且由于字符串较短,可以不理会浪费的空间,仅仅只是想简单地将短字符和宽字符相互转换,那有没有什么简便的方法呢?<br/>&nbsp;&nbsp;&nbsp;WIN32&nbsp;API里没有符合这种要求的函数,但我们可以自己进行封装:<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;//-------------------------------------------------------------------------------------<br/>&nbsp;&nbsp;//Description:<br/>&nbsp;&nbsp;//&nbsp;This&nbsp;function&nbsp;maps&nbsp;a&nbsp;character&nbsp;string&nbsp;to&nbsp;a&nbsp;wide-character&nbsp;(Unicode)&nbsp;string<br/>&nbsp;&nbsp;//<br/>&nbsp;&nbsp;//Parameters:<br/>&nbsp;&nbsp;//&nbsp;lpcszStr:&nbsp;&nbsp;Pointer&nbsp;to&nbsp;the&nbsp;character&nbsp;string&nbsp;to&nbsp;be&nbsp;converted&nbsp;<br/>&nbsp;&nbsp;//&nbsp;lpwszStr:&nbsp;&nbsp;Pointer&nbsp;to&nbsp;a&nbsp;buffer&nbsp;that&nbsp;receives&nbsp;the&nbsp;translated&nbsp;string.&nbsp;<br/>&nbsp;&nbsp;//&nbsp;dwSize:&nbsp;&nbsp;Size&nbsp;of&nbsp;the&nbsp;buffer<br/>&nbsp;&nbsp;//<br/>&nbsp;&nbsp;//Return&nbsp;Values:<br/>&nbsp;&nbsp;//&nbsp;TRUE:&nbsp;Succeed<br/>&nbsp;&nbsp;//&nbsp;FALSE:&nbsp;Failed<br/>&nbsp;&nbsp;//&nbsp;<br/>&nbsp;&nbsp;//Example:<br/>&nbsp;&nbsp;//&nbsp;MByteToWChar(szA,szW,sizeof(szW)/sizeof(szW));<br/>&nbsp;&nbsp;//---------------------------------------------------------------------------------------<br/>&nbsp;&nbsp;BOOL&nbsp;MByteToWChar(LPCSTR&nbsp;lpcszStr,&nbsp;LPWSTR&nbsp;lpwszStr,&nbsp;DWORD&nbsp;dwSize)<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Get&nbsp;the&nbsp;required&nbsp;size&nbsp;of&nbsp;the&nbsp;buffer&nbsp;that&nbsp;receives&nbsp;the&nbsp;Unicode&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;string.&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;dwMinSize;<br/>&nbsp;&nbsp;&nbsp;&nbsp;dwMinSize&nbsp;=&nbsp;MultiByteToWideChar&nbsp;(CP_ACP,&nbsp;0,&nbsp;lpcszStr,&nbsp;-1,&nbsp;NULL,&nbsp;0);<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;if(dwSize&nbsp;&lt;&nbsp;dwMinSize)<br/>&nbsp;&nbsp;&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;FALSE;<br/>&nbsp;&nbsp;&nbsp;&nbsp;}<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Convert&nbsp;headers&nbsp;from&nbsp;ASCII&nbsp;to&nbsp;Unicode.<br/>&nbsp;&nbsp;&nbsp;&nbsp;MultiByteToWideChar&nbsp;(CP_ACP,&nbsp;0,&nbsp;lpcszStr,&nbsp;-1,&nbsp;lpwszStr,&nbsp;dwMinSize);&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;TRUE;<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;//-------------------------------------------------------------------------------------<br/>&nbsp;&nbsp;//Description:<br/>&nbsp;&nbsp;//&nbsp;This&nbsp;function&nbsp;maps&nbsp;a&nbsp;wide-character&nbsp;string&nbsp;to&nbsp;a&nbsp;new&nbsp;character&nbsp;string<br/>&nbsp;&nbsp;//<br/>&nbsp;&nbsp;//Parameters:<br/>&nbsp;&nbsp;//&nbsp;lpcwszStr:&nbsp;&nbsp;Pointer&nbsp;to&nbsp;the&nbsp;character&nbsp;string&nbsp;to&nbsp;be&nbsp;converted&nbsp;<br/>&nbsp;&nbsp;//&nbsp;lpszStr:&nbsp;&nbsp;Pointer&nbsp;to&nbsp;a&nbsp;buffer&nbsp;that&nbsp;receives&nbsp;the&nbsp;translated&nbsp;string.&nbsp;<br/>&nbsp;&nbsp;//&nbsp;dwSize:&nbsp;&nbsp;Size&nbsp;of&nbsp;the&nbsp;buffer<br/>&nbsp;&nbsp;//<br/>&nbsp;&nbsp;//Return&nbsp;Values:<br/>&nbsp;&nbsp;//&nbsp;TRUE:&nbsp;Succeed<br/>&nbsp;&nbsp;//&nbsp;FALSE:&nbsp;Failed<br/>&nbsp;&nbsp;//&nbsp;<br/>&nbsp;&nbsp;//Example:<br/>&nbsp;&nbsp;//&nbsp;MByteToWChar(szW,szA,sizeof(szA)/sizeof(szA));<br/>&nbsp;&nbsp;//---------------------------------------------------------------------------------------<br/>&nbsp;&nbsp;BOOL&nbsp;WCharToMByte(LPCWSTR&nbsp;lpcwszStr,&nbsp;LPSTR&nbsp;lpszStr,&nbsp;DWORD&nbsp;dwSize)<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;DWORD&nbsp;dwMinSize;<br/>&nbsp;&nbsp;&nbsp;dwMinSize&nbsp;=&nbsp;WideCharToMultiByte(CP_OEMCP,NULL,lpcwszStr,-1,NULL,0,NULL,FALSE);<br/>&nbsp;&nbsp;&nbsp;if(dwSize&nbsp;&lt;&nbsp;dwMinSize)<br/>&nbsp;&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;FALSE;<br/>&nbsp;&nbsp;&nbsp;}<br/>&nbsp;&nbsp;&nbsp;WideCharToMultiByte(CP_OEMCP,NULL,lpcwszStr,-1,lpszStr,dwSize,NULL,FALSE);<br/>&nbsp;&nbsp;&nbsp;return&nbsp;TRUE;<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;使用方法也很简单,示例如下:<br/>&nbsp;&nbsp;wchar_t&nbsp;wText&nbsp;=&nbsp;{L"函数示例"};<br/>&nbsp;&nbsp;char&nbsp;sText=&nbsp;{0};<br/>&nbsp;&nbsp;WCharToMByte(wText,sText,sizeof(sText)/sizeof(sText));<br/>&nbsp;&nbsp;MByteToWChar(sText,wText,sizeof(wText)/sizeof(wText));<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;这两个函数的缺点在于无法动态分配内存,在转换很长的字符串时可能会浪费较多内存空间;优点是,在不考虑浪费空间的情况下转换较短字符串非常方便.<br/></div></div></td></tr></tbody></table>

wzz1968 发表于 2007-5-17 11:44:00

<p>将实数转化为字符的方法用标准的C语言函数,sprintf(sTemp,"%0.2f",val);</p><p>然后在需要输入文字的地方用sTemp代替即可.</p><p></p>
页: [1]
查看完整版本: 该死的Unicode