Multi-Byte/Unicode支持总结

很多时候,我们的程序需要支持两种字符集,Multi-Byte与Unicode。

这时就需要进行字符串转换,以及两套API之间的切换。


对两种字符集的统一支持,本文做了一些简单总结。


1、常用转换函数

以下四个函数为Multibyte/Unicode转化基本函数,已处理内存泄露问题。

其他转换可以在这四个基本函数基础上操作。

1.1. Ansi转化为Unicode

  1. static wstring AnsiToUnicode(const string& str)  
  2. {  
  3.     int  unicodeLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);  
  4.     if (unicodeLen <= 0)  
  5.     {  
  6.         return L"";  
  7.     }  
  8.   
  9.     wchar_t* pUnicode = new wchar_t[unicodeLen+1];  
  10.     memset(pUnicode, 0, (unicodeLen+1)*sizeof(wchar_t));  
  11.   
  12.     ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen);  
  13.   
  14.     wstring rt = pUnicode;  
  15.   
  16.     delete[] pUnicode;  
  17.     pUnicode = NULL;  
  18.   
  19.     return rt;  
  20. }  
static wstring AnsiToUnicode(const string& str)
{
    int  unicodeLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
    if (unicodeLen <= 0)
    {
        return L"";
    }

    wchar_t* pUnicode = new wchar_t[unicodeLen+1];
    memset(pUnicode, 0, (unicodeLen+1)*sizeof(wchar_t));

    ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen);

    wstring rt = pUnicode;

    delete[] pUnicode;
    pUnicode = NULL;

    return rt;
}

1.2. Unicode转化为Ansi

  1. static string UnicodeToAnsi(const wstring& str)  
  2. {  
  3.     int iTextLen = WideCharToMultiByte(CP_ACP, 0, str.c_str(), -1, NULL, 0, NULL, NULL);  
  4.   
  5.     if (iTextLen <= 0)  
  6.     {  
  7.         return "";  
  8.     }  
  9.   
  10.     char* pElementText = new char[iTextLen + 1];  
  11.     memset(pElementText, 0, sizeof(char)*(iTextLen + 1));  
  12.   
  13.     ::WideCharToMultiByte(CP_ACP, 0, str.c_str(), -1, pElementText, iTextLen, NULL, NULL);  
  14.   
  15.     string strText = pElementText;  
  16.       
  17.     delete[] pElementText;  
  18.     pElementText = NULL;  
  19.       
  20.     return strText;  
  21. }  
static string UnicodeToAnsi(const wstring& str)
{
    int iTextLen = WideCharToMultiByte(CP_ACP, 0, str.c_str(), -1, NULL, 0, NULL, NULL);

    if (iTextLen <= 0)
    {
        return "";
    }

    char* pElementText = new char[iTextLen + 1];
    memset(pElementText, 0, sizeof(char)*(iTextLen + 1));

    ::WideCharToMultiByte(CP_ACP, 0, str.c_str(), -1, pElementText, iTextLen, NULL, NULL);

    string strText = pElementText;
    
    delete[] pElementText;
    pElementText = NULL;
    
    return strText;
}

1.3. UTF-8 to Unicode

  1. static wstring UTF8ToUnicode(const string& str)  
  2. {  
  3.     int unicodeLen = ::MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);  
  4.   
  5.     if (unicodeLen <= 0)  
  6.     {  
  7.         return L"";  
  8.     }  
  9.   
  10.     wchar_t* pUnicode = new  wchar_t[unicodeLen+1];  
  11.     memset(pUnicode, 0, (unicodeLen+1)*sizeof(wchar_t));  
  12.   
  13.     ::MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen);  
  14.   
  15.     wstring  rt = pUnicode;  
  16.   
  17.     delete[] pUnicode;  
  18.     pUnicode = NULL;  
  19.   
  20.     return rt;    
  21. }  
static wstring UTF8ToUnicode(const string& str)
{
    int unicodeLen = ::MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);

    if (unicodeLen <= 0)
    {
        return L"";
    }

    wchar_t* pUnicode = new  wchar_t[unicodeLen+1];
    memset(pUnicode, 0, (unicodeLen+1)*sizeof(wchar_t));

    ::MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen);

    wstring  rt = pUnicode;

    delete[] pUnicode;
    pUnicode = NULL;

    return rt;  
}

1.4. Unicode to UTF-8

  1. static string UnicodeToUTF8(const wstring& str )  
  2. {  
  3.     int iTextLen = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, NULL, 0, NULL, NULL);  
  4.   
  5.     if (iTextLen <= 0)  
  6.     {  
  7.         return "";  
  8.     }  
  9.   
  10.     char* pElementText = new char[iTextLen + 1];  
  11.     memset(pElementText, 0, sizeof(char) * (iTextLen + 1));  
  12.     ::WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, pElementText, iTextLen, NULL, NULL);  
  13.   
  14.     string strText = pElementText;  
  15.   
  16.     delete[] pElementText;  
  17.     pElementText = NULL;  
  18.   
  19.     return strText;  
  20. }  
static string UnicodeToUTF8(const wstring& str )
{
    int iTextLen = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, NULL, 0, NULL, NULL);

    if (iTextLen <= 0)
    {
        return "";
    }

    char* pElementText = new char[iTextLen + 1];
    memset(pElementText, 0, sizeof(char) * (iTextLen + 1));
    ::WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, pElementText, iTextLen, NULL, NULL);

    string strText = pElementText;

    delete[] pElementText;
    pElementText = NULL;

    return strText;
}


2、通用宏使用

MultiByte/Unicode两套字符集下使用API不同,目前已有通用宏来解决此问题。

具体相关宏如下表所示。

通用宏定义

MultiByte API

Unicode API

功能说明

TCHAR

char

wchar_t

字符定义

_ttoi

atoi

_wtoi

字符串转int

_tstof

atof

_wtof

字符串转float

_ttoi64

_atoi64

_wtoi64

字符串转64位整型

_itot

itoa

_itow

int转字符串

_tstof

gcvt

_wtof

float转字符串

_tcslen

strlen

wcslen

获取字符串长度

_tcsstr

strstr

wcsstr

子字符串截取

_tcscpy

strcpy

wcscpy

字符串复制

建议使用_tcscpy_s

_stprintf_s

sprintf_s

swprintf_s

字符串赋值


例如,把字符串“123”转换为int型,直接调用_ttoi(_T(“123”))即可,在Multibtye/Unicode字符集下都能正常运行。


3、控制台字符串输出

  1. CString strTest = _T(“测试”);  
  2. #ifdef UNICODE   
  3. wcout <<(const wchar_t*)strTest <<endl;  
  4. #else   
  5. cout <<(const char*)strTest <<endl;  
  6. #endif  



原文地址:http://blog.csdn.net/segen_jaa/article/details/7550317
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值