string 与wchar_t* char*

  • Post author:
  • Post category:其他


string TCHAR2char(const TCHAR* STR)
{
    string strchar;
    if (!*STR)
    {
        return strchar;
    }

    //返回字符串的长度
    int size = WideCharToMultiByte(CP_ACP, 0, STR, -1, NULL, 0, NULL, FALSE);

    //申请一个多字节的字符串变量
    char* str = new char[size + 1];

    //将STR转成str
    WideCharToMultiByte(CP_ACP, 0, STR, -1, str, size, NULL, FALSE);
    str[size] = '\0';
    strchar = str;
    delete[] str;

    return strchar;
}


TCHAR* char2TCAHR(const char* str)
{
    int size = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
    TCHAR* retStr = new TCHAR[size + 1];
    MultiByteToWideChar(CP_ACP, 0, str, -1, retStr, size);
    retStr[size] = '\0';
    return retStr;
}



版权声明:本文为chengriyue原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。