• VC++ 中使用 std::string 转换字符串编码


    1说明    1

    1.1 代码    1

    1.2 使用    4

    1说明

    VC++中宽窄字符串的相互转换比较麻烦,借助std::string能大大减少代码量。

    1.1 代码

    函数声明如下:

    std::string stringA2W(const char* pA,int nA,UINT uCodePage = CP_ACP);

    std::string stringW2A(const wchar_t*pW,int nW,UINT uCodePage = CP_ACP);

    std::string stringA2W(const std::string&sA,UINT uCodePage = CP_ACP);

    std::string stringW2A(const std::string&sW,UINT uCodePage = CP_ACP);

    函数实现如下:

    /***************************************************************

    窄字符串 ==> 宽字符串(UTF16LE

    pA [in] 窄字符串首地址

    nA [in] 窄字符串长度。小于零就使用 strlen 计算长度。

    uCodePage [in] 窄字符串的代码页

    如:CP_ACP 表示系统默认;936 表示 GBK……

    返回:

    宽字符串 sW

    宽字符串的字符数 nChar = sW.length() / sizeof(wchar_t)

    宽字符串的字节数 nByte = (nChar + 1) * sizeof(wchar_t) - 1

    字节数多加了 sizeof(wchar_t) - 1 = 1 ,是为了下面的用法

    const wchar_t* pW = (const wchar_t*)sW.c_str();

    ***************************************************************/

    std::string stringA2W(const char*pA,int nA,UINT uCodePage)

    {

    std::string sW;

    if(pA)

    {

    if(nA < 0)

    {

    nA = strlen(pA);

    }

    if(nA > 0)

    {

    int nW = MultiByteToWideChar(uCodePage,0,pA,nA,NULL,0);

    if(nW > 0)

    {

    int nByte = (nW + 1) * sizeof(wchar_t);

    wchar_t*pW = (wchar_t*)malloc(nByte);

    if(pW)

    {

    MultiByteToWideChar(uCodePage,0,pA,nA,pW,nW);

    pW[nW] = L'';

    sW.assign((const char*)pW,nByte - 1);

    free(pW);

    }

    }

    }

    }

    if(sW.empty())

    {

    sW = std::string(sizeof(wchar_t) - 1,'');

    }

    return sW;

    }

    /***************************************************************

    窄字符串 ==> 宽字符串(UTF16LE

    sA [in] 窄字符串

    uCodePage [in] 窄字符串的代码页

    如:CP_ACP 表示系统默认;936 表示 GBK……

    返回:宽字符串

    ***************************************************************/

    std::string stringA2W(const std::string&sA,UINT uCodePage)

    {

    return stringA2W(sA.c_str(),sA.length(),uCodePage);

    }

    /***************************************************************

    宽字符串(UTF16LE ==> 窄字符串

    pW [in] 宽字符串首地址

    nW [in] 宽字符串字符数。小于零就使用 wcslen 计算长度。

    uCodePage [in] 窄字符串的代码页

    如:CP_ACP 表示系统默认;936 表示 GBK……

    返回:窄字符串

    ***************************************************************/

    std::string stringW2A(const wchar_t*pW,int nW,UINT uCodePage)

    {

    std::string sA;

    if(pW)

    {

    if(nW < 0)

    {

    nW = wcslen(pW);

    }

    if(nW > 0)

    {

    int nA = WideCharToMultiByte(uCodePage,0,pW,nW

    ,NULL,NULL,NULL,NULL);

    if(nA > 0)

    {

    char*pA = (char*)malloc(nA);

    if(pA)

    {

    WideCharToMultiByte(uCodePage,0,pW,nW

    ,pA,nA,NULL,NULL);

    sA.assign(pA,nA);

    free(pA);

    }

    }

    }

    }

    return sA;

    }

    /***************************************************************

    宽字符串(UTF16LE ==> 窄字符串

    sW [in] 宽字符串,编码为 UTF16LE

    uCodePage [in] 窄字符串的代码页

    如:CP_ACP 表示系统默认;936 表示 GBK……

    返回:窄字符串

    ***************************************************************/

    std::string stringW2A(const std::string&sW,UINT uCodePage)

    {

    return stringW2A((const wchar_t*)sW.c_str()

    ,sW.length() / sizeof(wchar_t),uCodePage);

    }

    1.2 使用

    有了上述四个函数,字符串的编码转换用一、两行代码即可实现。

    如:将GBK字符串"测试"转换为宽字符串

    std::string sW = stringA2W("测试");

    //简体中文 Windows

    std::string sW = stringA2W("测试",936);

    //安装有代码页936Windows

    如:将GBK字符串"测试"转换为UTF-8编码

    std::string sUTF8 = stringW2A(stringA2W("测试",936),CP_UTF8);

    如:将GBK字符串"测试"转换为Big5编码

    std::string sBig5 = stringW2A(stringA2W("测试",936),950);

  • 相关阅读:
    linux死锁检测的一种思路【转】
    2016 最佳 Linux 发行版排行榜【转】
    kernel 3.10内核源码分析--TLB相关--TLB概念、flush、TLB lazy模式 【转】
    spin_lock & mutex_lock的区别? 【转】
    Linux输入子系统:多点触控协议 -- multi-touch-protocol.txt【转】
    Linux时间子系统之七:定时器的应用--msleep(),hrtimer_nanosleep()【转】
    ARM Linux 3.x的设备树(Device Tree)【转】
    设备树概述【转】
    Tslib的移植【转】
    带你制作百词斩单词表读写插件
  • 原文地址:https://www.cnblogs.com/hanford/p/6116394.html
Copyright © 2020-2023  润新知