Benjamin

静以修身,俭以养德,非澹薄无以明志,非宁静无以致远。
随笔 - 386, 文章 - 0, 评论 - 196, 引用 - 0
数据加载中……

WCHAT、TCHAT、CString和char*的转换(WM5.0)

在平时的编程中字符串转换经常遇到,常用的是MultiByteToWideChar和WideCharToMultiByte,具体的函数如下:
/////////////////////////////////////////////////////////////////////////////
//Description:
// This function maps a wide-character string to a new character string
//
//Parameters:
// lpcwszStr: [in] Pointer to the character string to be converted
// lpszStr: [out] Pointer to a buffer that receives the translated string.
// dwSize: [in] Size of the buffer
//
//Return Values:
// TRUE: Succeed
// FALSE: Failed
//
//Example:
// MByteToWChar(szW,szA,sizeof(szA)/sizeof(szA[0]));
//////////////////////////////////////////////////////////////////////////////
BOOL WCharToMByte(LPCWSTR lpcwszStr, LPSTR lpszStr, DWORD dwSize)
{
 DWORD dwMinSize;
 dwMinSize = WideCharToMultiByte(CP_OEMCP,NULL,lpcwszStr,-1,NULL,0,NULL,FALSE);
 if(dwSize < dwMinSize)
 {
  return FALSE;
 }
 WideCharToMultiByte(CP_OEMCP,NULL,lpcwszStr,-1,lpszStr,dwSize,NULL,FALSE);
 return TRUE;
}
///////////////////////////////////////////////////////////////////////////////
//Description:
// This function maps a character string to a wide-character (Unicode) string
//
//Parameters:
// lpcszStr: [in] Pointer to the character string to be converted
// lpwszStr: [out] Pointer to a buffer that receives the translated string.
// dwSize: [in] Size of the buffer
//
//Return Values:
// TRUE: Succeed
// FALSE: Failed
//
//Example:
// MByteToWChar(szA,szW,sizeof(szW)/sizeof(szW[0]));
///////////////////////////////////////////////////////////////////////////////
BOOL MByteToWChar(LPCSTR lpcszStr, LPWSTR lpwszStr, DWORD dwSize)
{
    // Get the required size of the buffer that receives the Unicode
    // string.
    DWORD dwMinSize;
    dwMinSize = MultiByteToWideChar (CP_ACP, 0, lpcszStr, -1, NULL, 0);
 
    if(dwSize < dwMinSize)
    {
  return FALSE;
    }
 
    // Convert headers from ASCII to Unicode.
    MultiByteToWideChar (CP_ACP, 0, lpcszStr, -1, lpwszStr, dwMinSize); 
    return TRUE;
}
很少有人提及到下面的两个函数size_t  wcstombs(char (&mbstr)[size], const wchar_t *wcstr,  size_t count );
size_t mbstowcs( wchar_t (&wcstr)[size], const char *mbstr, size_t count ); 以为只能用在COM中,其实不然,还可以用在WM5.0和Wince平台;
用这两个函数的好处就是一行代码就可以完成转换。要注意的是在用wcstombs函数时第三个参数必须是第二个参数长度的两倍,用mbstowcs是第三个参数是第二个参数的长度,wcstombs可以完成WCHAR、TCHAR、CString向char*的转换。
CString是MFC中的类,可以用WCHAR、TCHAR、cahr*直接初始化和赋值;值得一提的是在WM5.0、WCE平台稍微有些和其他windows平台不同的是,虽然函数传参时无论WCHAR/TCHAR、char*都可以转成CString作参数传递,因为函数传参实际是一个赋值的过程。但是在输出到设备上时,还必须用mbstowcs函数来转换。
虽然Wce和Mobile平台上只能用Unicode,但是读写文件时依然可以用标准C的文件操作函数,但是读写文件时用的字符要一致。
关于VS2008平台字符串转换,请看http://msdn.microsoft.com/zh-cn/library/ms235631(VS.80).aspx

posted on 2008-08-21 22:30 Benjamin 阅读(4212) 评论(26)  编辑 收藏 引用 所属分类: PDA/PPC开发

评论

# re: WCHAT、TCHAT、CString和char*的转换(WM5.0)  回复  更多评论   

The <a href="http://lowest-rate-loans.com/topics/personal-loans">personal loans</a> seem to be useful for guys, which would like to organize their own organization. As a fact, it is very comfortable to receive a secured loan.
2010-05-24 17:21 | HendricksRobin

# re: WCHAT、TCHAT、CString和char*的转换(WM5.0)  回复  更多评论   

There’re billions of different people, which know about your very good release, and I offer to order the monophonic ringtones or mp3 ringtones to do yourself unique.
2010-07-18 03:18 | ringtones

# re: WCHAT、TCHAT、CString和char*的转换(WM5.0)  回复  更多评论   

Fine information, many thanks to the author. It is puzzling to me now, but in general, the usefulness and importance is overwhelming. Very much thanks again and good luck!
2010-12-23 14:37 | Funny Videos

# dating website  回复  更多评论   

这是一个很有趣的文章。这真的峰我的兴趣进一步阅读你的文章。谢谢分享。
2011-01-10 16:16 | dating website

# top seo firm  回复  更多评论   

It is good to visit a website and learn something new like your blog. Thanks for the effort!
2011-02-11 15:23 | top seo firm

# webcialisadvisor  回复  更多评论   

Thanks for the useful tips and information from the author. I felt good to read all the previous postings too. I appreciate the time and effort that the author put to share all these information to us in an interesting and impressive way. Looking for more posts like this.
2011-03-15 19:36 | Finbar

# Really a great post.Reading is my passion and i like reading this type of stuff always.Nice sharing friend  回复  更多评论   

Really a great post.Reading is my passion and i like reading this type of stuff always.Nice sharing friend
2011-04-05 15:50 | mbt uk

# re: WCHAT、TCHAT、CString和char*的转换(WM5.0)  回复  更多评论   

유용한 팁 및 정보 저자로부터 주셔서 감사합니다. 나 역시 이전의 모든 게시물을 읽고 좋은 느낌. 내가 시간과 작성자가 흥미롭고 인상적인 방식으로 우리에게이 모든 정보를 공유할 넣어 그 노력에 감사드립니다. 이렇게 더 많은 게시물을 찾으.
2011-06-10 09:21 | revabs workout program

# re: WCHAT、TCHAT、CString和char*的转换(WM5.0)  回复  更多评论   

Very well written. This is the kind of information that is useful to those want to increase their SERP’s. Keep up the good work.

<a href="http://coppervalleyrestorationservices.com">water">http://coppervalleyrestorationservices.com">water damage</a>
<a href="http://coppervalleyrestorationservices.com">fire damage</a>
<a href="http://coppervalleyrestorationservices.com">mold">http://coppervalleyrestorationservices.com">mold removal</a>
<a href="http://coppervalleyrestorationservices.com">mold">http://coppervalleyrestorationservices.com">mold abatement</a>
<a href="http://coppervalleyrestorationservices.com">water">http://coppervalleyrestorationservices.com">water restoration</a>
2011-08-14 14:27 | kadambi

# re: WCHAT、TCHAT、CString和char*的转换(WM5.0)  回复  更多评论   

I like periodicals tremendously because I discovered lots of information from it. It is now that I realize that I possibly could also score something meaningful on the internet. Thanks for making this fantastic web page.

# re: WCHAT、TCHAT、CString和char*的转换(WM5.0)  回复  更多评论   

I seemed to be exceedingly agitated to go through the affirmations in this internet site because the given topic is seriously wonderful. It will bag the concentration of all web navigators.
2011-11-07 17:30 | Business Plan Writing

# re: WCHAT、TCHAT、CString和char*的转换(WM5.0)  回复  更多评论   

你寫的東西肯定是我有興趣英寸的方式提出你的立場是,產生少量無欽佩。出於這個原因,我選擇,讓你知道這些事情,因為我覺得你應該知道。這是我在說什麼。
2012-01-16 01:32 | Cell Phone Lookup

# re: WCHAT、TCHAT、CString和char*的转换(WM5.0)  回复  更多评论   

I’ve been surfing for several kinds of training over the net at the time I noticed this specific Mobile Crane Training site. I was obviously astonished by the post. I'm now learning new from you. Keep writing cool information towards your site.

2012-03-30 14:04 | Mobile Crane Training

# re: WCHAT、TCHAT、CString和char*的转换(WM5.0)  回复  更多评论   

One of my pastimes is to surf for nice blog sites online. This is why I saw this blog site on annuity rates. Your blog site right here is most likely one of my favorites. !
2012-10-02 11:07 | annuity rates

# re: WCHAT、TCHAT、CString和char*的转换(WM5.0)  回复  更多评论   

I at first go online to search for details on psychic Adelaide however I was led to This site given that it has a cppblog that is fascinating. I was impressed after reviewing the whole write-up. Incredible!
2012-10-23 09:24 | This site

# re: WCHAT、TCHAT、CString和char*的转换(WM5.0)  回复  更多评论   

I had great time over here and i found plenty of things that are pleasant and exciting.I had lot of fun and entertainment over here
2012-11-03 21:20 | b2b trade

# re: WCHAT、TCHAT、CString和char*的转换(WM5.0)  回复  更多评论   

When I am finished with all of my household duties, I like to spend time online. I merely completed reading a post on Business Consultant when I saw the hyperlink to your web page. I believe it is fantastic to review useful reasons provided by true people.
2013-01-08 10:03 | Business Consultant

# re: WCHAT、TCHAT、CString和char*的转换(WM5.0)  回复  更多评论   

Recently, I saw this blog on Waterloo Structures which caught my attention. Its impressive write-up and I have NO DOUBT! Check this website for more information.
2013-03-02 09:54 | Check this website

# re: WCHAT、TCHAT、CString和char*的转换(WM5.0)  回复  更多评论   

After buying magnetic signs on the net, I stumbled across your web site and read through the WCHAT. I find it very awesome.
2013-03-04 09:38 | magnetic signs

# re: WCHAT、TCHAT、CString和char*的转换(WM5.0)  回复  更多评论   

Your WCHAT is very impressive and filled with insights. Truthfully, I accidentally found it after checking out the site regarding GVO. Check it out by visiting gvoteamelite.com.
2013-03-05 11:46 | gvoteamelite.com

# re: WCHAT、TCHAT、CString和char*的转换(WM5.0)  回复  更多评论   

Whilst seeking for columns pertaining to how to learn to trade forex, I happen to review WCHAT. And I accept, it is amongst the most ideal that I have read yet.
2013-05-16 15:30 | Learn to trade forex

# re: WCHAT、TCHAT、CString和char*的转换(WM5.0)  回复  更多评论   

Using my smartphone, I've been taking a look at cppblog whilst waiting for my buddy I accompanied right here at HAIR SALON SILVER LAKE BY HTTP://SILVERLAKESTUDIOLA.COM/. It could however take her a moment so I sense I'll be socializing a minute more.
2013-10-30 09:22 | http://silverlakestudiola.com

# re: WCHAT、TCHAT、CString和char*的转换(WM5.0)  回复  更多评论   

I've had an exhausting time searching Josh Groban records on ITunes Visit http://www.soundbarreviewsguide.com/. Glad I happen to go over several intriguing factors at your blog; Right now I get more potency.
2014-02-14 11:10 | Randal Porter

# re: WCHAT、TCHAT、CString和char*的转换(WM5.0)  回复  更多评论   

Glad I happen to go over several intriguing factors at your blog; Right now I get more potency.
2016-08-11 23:48 | http://delhicosmeticsurgery.in

# re: WCHAT、TCHAT、CString和char*的转换(WM5.0)  回复  更多评论   

I had the pleasure to chat with Cristina recently and got to know a little bit more about her life as a jewelry designer, wife and mother.
2016-08-11 23:51 | http://bestdelhispa.com

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理