CStringLocaleHelper : 一个用于UTF8和UNICODE字符串转换的辅助类

本文介绍了一个名为 CStringLocaleHelper 的类,用于简化在不同字符集(如 UTF-8 和 Unicode)间的字符串转换过程。该类封装了 Windows API 中的 WideCharToMultiByte 和 MultiByteToWideChar 函数,并提供了多种类型转换操作符。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PomeloWu原创©,转载请注明出处

微软抛弃MBCS看来是必然的选择。VS2005默认创建的工程都是UNICODE的。但无论是std::string/std::wstring还是CString,都没有提供不同字符集间转换的包装。

写CStringLocaleHelper本意是辅助Linux和Windows通讯时UTF-8和UNICODE的转换,实际上对它稍加改变,就能面向更多的code page,让人从WINDOWS程序中无趣的字符集转换之中解放出来。

CStringLocaleHelper其实就是对调用WideCharToMultiByte/MultiByteToWideChar的封装,可以返回指定字符集(目前是UTF-8或者UNICODE)的字符串或者std::string和std::wstring。此外,重载了针对LPCTSTR、std::string和std::wstring的operator (),方便类型强制转化;重载了operator =以便增加CStringLocaleHelper的灵活性。


 以下是具体代码:

/* ------------------------------------------------------------- @filename: StringLocaleHelper.h @author: Pomelo Wu @purpose: A helper string class, handling string conversions between UTF-8 and UNICODE @revision history: -- Date Ver. Modifier Description-- 2006/03/15 0.1 Pomelo created 2006/03/29 0.2 Pomelo return type modified 2006/04/03 0.3 Pomelo cast operator added // -----------------------------------------------------------*/ #ifndef __STRINGLOCALEHELPER_H__ #define __STRINGLOCALEHELPER_H__ using namespace std; class CStringLocaleHelper { public: CStringLocaleHelper(); CStringLocaleHelper(string str); CStringLocaleHelper(wstring str); ~CStringLocaleHelper(void); public: const char * GetLocaleBuffer() const { return m_strLocale.c_str(); } const wchar_t * GetUnicodeBuffer() const { return m_strUnicode.c_str(); } const string& GetLocaleString() const { return m_strLocale; } const wstring& GetUnicodeString() const { return m_strUnicode; } operator LPCTSTR () const; operator string () const; operator wstring () const; CStringLocaleHelper & operator = (string strSrc); CStringLocaleHelper & operator = (wstring strSrc); protected: string m_strLocale; wstring m_strUnicode; }; #endif // __STRINGLOCALEHELPER_H__ /* ------------------------------------------------------------- @filename: StringLocaleHelper.cpp @author: Pomelo Wu @purpose: The implementation of CStringLocaleHelper @revision history: -- Date Ver. Modifier Description-- 2006/03/15 0.1 Pomelo created 2006/03/29 0.2 Pomelo return type modified 2006/04/03 0.3 Pomelo cast operator added // -----------------------------------------------------------*/ // ----------------------- Using strings ----------------------- #include <string> // --------------------- Unicode functions --------------------- #include <Windows.h> #include "StringLocaleHelper.h" ////////////////////////////////////////////////////////////////////////// // // native functions int Unicode2Locale(const wchar_t *pSrc, char *pDest = NULL, int nCount = 0) { if (pSrc == NULL) // invalid parameter return -1; if (pDest == NULL) // queries the count return WideCharToMultiByte(CP_UTF8, 0, pSrc, -1, NULL, 0, NULL, NULL); memset(pDest, 0, sizeof(char) * (nCount + 1)); return WideCharToMultiByte(CP_UTF8, 0, pSrc, -1, pDest, nCount, NULL, NULL); } int Locale2Unicode(const char *pSrc, wchar_t *pDest = NULL, int nCount = 0) { if (pSrc == NULL) // invalid parameter return -1; if (pDest == NULL) // queries the count return MultiByteToWideChar(CP_UTF8, NULL, pSrc, -1, NULL, 0); memset(pDest, 0, sizeof(wchar_t) * (nCount + 1)); return MultiByteToWideChar(CP_UTF8, NULL, pSrc, -1, pDest, nCount + 1); } ////////////////////////////////////////////////////////////////////////// // // constructors / destructors CStringLocaleHelper::CStringLocaleHelper() { m_strLocale.clear(); m_strUnicode.clear(); } CStringLocaleHelper::CStringLocaleHelper(string str) { m_strLocale = str; int nCount = Locale2Unicode(m_strLocale.c_str()); wchar_t *pwcText = new wchar_t [nCount + 1]; if (pwcText) { nCount = Locale2Unicode(str.c_str(), pwcText, nCount); m_strUnicode = pwcText; delete [] pwcText; } } CStringLocaleHelper::CStringLocaleHelper(wstring str) { m_strUnicode = str; int nCount = Unicode2Locale(str.c_str()); char *pBufferAnsi = new char[nCount + 1]; if (pBufferAnsi) { nCount = Unicode2Locale(str.c_str(), pBufferAnsi, nCount); m_strLocale = pBufferAnsi; delete [] pBufferAnsi; } } CStringLocaleHelper::~CStringLocaleHelper(void) { } ////////////////////////////////////////////////////////////////////////// // // operators CStringLocaleHelper::operator LPCTSTR() const { #ifdef _UNICODE return m_strUnicode.c_str(); #else return m_strLocale.c_str(); #endif } CStringLocaleHelper::operator string () const { return m_strLocale; } CStringLocaleHelper::operator wstring () const { return m_strUnicode; } CStringLocaleHelper & CStringLocaleHelper::operator =(std::string strSrc) { m_strLocale = strSrc; int nCount = Locale2Unicode(strSrc.c_str()); wchar_t *pwcText = new wchar_t [nCount + 1]; if (pwcText) { nCount = Locale2Unicode(strSrc.c_str(), pwcText, nCount); m_strUnicode = pwcText; delete [] pwcText; } return *this; } CStringLocaleHelper & CStringLocaleHelper::operator =(std::wstring strSrc) { m_strUnicode = strSrc; int nCount = Unicode2Locale(strSrc.c_str()); char *pcText = new char [nCount + 1]; if (pcText) { nCount = Unicode2Locale(strSrc.c_str(), pcText, nCount); m_strLocale = pcText; delete [] pcText; } return *this; }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值