std::wstring std::string w2m m2w

static std::wstring m2w(std::string ch, unsigned int CodePage = CP_ACP)
{
    if (ch.empty())return L"";
    std::wstring ret;
    DWORD dwOutSize = 0;
    dwOutSize = MultiByteToWideChar(CodePage, 0, ch.c_str(), -1, NULL, 0);

    ret.resize(dwOutSize - 1);
    MultiByteToWideChar(CodePage, 0, ch.c_str(), ch.size(), &ret[0], dwOutSize);

    return ret;
}
static std::string w2m(std::wstring wch, unsigned int CodePage = CP_ACP)
{
    std::string ret;
    DWORD dwOutSize = 0;
    dwOutSize = WideCharToMultiByte(CodePage, 0, wch.c_str(), -1, NULL, 0, NULL, FALSE);

    char *pwText = 0;
    pwText = new char[dwOutSize];
    pwText[dwOutSize - 1] = ‘\0‘;

    WideCharToMultiByte(CodePage, 0, wch.c_str(), wch.size(), pwText, dwOutSize, NULL, FALSE);

    ret = pwText;
    if (pwText)delete[]pwText;

    return ret;
}

std::string name = w2m(m2w(obj->GetName(), CP_UTF8));//转换编码

原文地址:https://www.cnblogs.com/herd/p/11140907.html

时间: 2024-08-10 02:56:31

std::wstring std::string w2m m2w的相关文章

对std::string和std::wstring区别的解释,807个赞同,有例子

807down vote string? wstring? std::string is a basic_string templated on a char, and std::wstring on a wchar_t. char vs. wchar_t char is supposed to hold a character, usually a 1-byte character. wchar_t is supposed to hold a wide character, and then,

std::wstring跨dll 崩溃

今天用个测试exe调用了个dll,有个接口返回std::wstring,经调试发现挂在该函数return之后,怀疑是string不适合作为返回值,百度一番发现下面这篇解释的很详细. STL跨平台调用会出现很多异常,你可以试试. STL使用模板生成,当我们使用模板的时候,每一个EXE,和DLL都在编译器产生了自己的代码,导致模板所使用的静态成员不同步,所以出现数据传递的各种问题,下面是详细解释. 原因分析: 一句话-----如果任何STL类使用了静态变量(无论是直接还是间接使用),那么就不要再写出

宽字符std::wstring的长度和大小问题?sizeof(std::wstring)是固定的32,说明std::wstring是一个普通的C++类,而且和Delphi不一样,没有负方向,因为那个需要编译器的支持

std::wstring ws=L"kkkk";    int il=ws.length();    int ia=sizeof(ws);    int ib=sizeof("dddd");    int ic=sizeof(L"kkkk");输出为    il=4,ia=32,ib=5,ic=10为什么ia=32 ?wstring到底对L"kkkk"做了什么? http://www.debugease.com/vc/2171

QString与中文,QString与std::wstring的相互转换(使用fromStdWString和u8关键字)

Qt版本:5.5.1 Qt的QString功能丰富,对非英语语言的支持也不是问题,但支持得不够直接.例如,像 ? 1 QString str("死亡使者赛维"); 这样直接用带中文的字符串进行构造,那么用QMessageBox显示str时将出现乱码.如果使用fromLocal8Bit.fromLatin1这样的函数,又依赖本地计算机的显示语言,所以它们不是好方法. 显式地使用宽字符(wchar_t)或UTF-8才是好方法. ? 1 2 QString str0(QString::fro

QString与std::wstring的转换问题

问题描述: 在qt工程中调用QString::fromStdWString()时,老是报错error LNK2019: 无法解析的外部符号...QString::fromStdWString(...)... 原因: 在Qt库中 wchar_t 不是内置类型,所以QT建议我们构建基于Qt的软件时,也不要将 wchar_t 作为内置类型.但是在一些情况下,其他的库构建时可能已经将 wchar_t 作为了内置类型.当使用 std::wstring.QString::toStdWString()和 QS

C++中Cstring、wstring 和string互相转换总结

通过前一篇文章<C++中string,wstring,CString的基本概念和用法>,对Cstring.wstring 和string有了一个了解.string是C++提供的标准字符串操作类.wstring是操作宽字符串的类..CString是对string(字符串)和wstring(宽字符串)的一个封装,常用在mfc中,用来解决编码问题的.在编程过程中,经常会遇到Cstring.wstring 和string之间的相互转换,在这里做了个简单地总结,另外也会附上其他类型的转换.常见的转换方式

C++中wstring和string的互相转换

1.wstring 转换为string #include <string> std::string ws2s(const std::wstring& ws) { std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C"; setlocale(LC_ALL, "chs"); const wchar_t* _Source = ws.c_str(); size_t _Dsiz

C++11 std::bind std::function 高级使用方法

从最基础的了解,std::bind和std::function /* * File: main.cpp * Author: Vicky.H * Email: [email protected] */ #include <iostream> #include <functional> #include <typeinfo> #include <string.h> int add1(int i, int j, int k) { return i + j + k;

c++11 std::ref std::cref

参考: C++已经有了引用操作符&为什么C++11还要引入std:ref std::ref和std::cref使用 &是类型说明符,而std::ref是一个函数,返回std::reference_wrapper(类似于指针) 为什么需要std::ref?(std::cref类似) 主要是考虑到c++11中的函数式编程,例如:std::bind std::bind在使用时,是对参数直接拷贝,而不是引用 发现这个问题的契机是在使用thread的标准库时 #include<iostream