同样单字符和宽字符转换的的C++代码,VC下正常,QT下异常

sty_app 2021-02-26 10:17:52
上QT源码:

#include <QCoreApplication>
#include <QDebug>
#include <iostream>
using namespace std;

std::string ws2s(const std::wstring &ws)
{
size_t i;
std::string curLocale = setlocale(LC_ALL, NULL);
setlocale(LC_ALL, "");
const wchar_t* _source = ws.c_str();
size_t _dsize = 2 * ws.size() + 1;
char* _dest = new char[_dsize];
memset(_dest, 0x0, _dsize);
wcstombs_s(&i, _dest, _dsize, _source, _dsize);
std::string result = _dest;
delete[] _dest;
setlocale(LC_ALL, curLocale.c_str());
return result;
}

std::wstring s2ws(const std::string &s)
{
size_t i;
std::string curLocale = setlocale(LC_ALL, NULL);
setlocale(LC_ALL, "chs");
const char* _source = s.c_str();
size_t _dsize = s.size() + 1;
wchar_t* _dest = new wchar_t[_dsize];
wmemset(_dest, 0x0, _dsize);
mbstowcs_s(&i, _dest, _dsize, _source, _dsize);
std::wstring result = _dest;
delete[] _dest;
setlocale(LC_ALL, curLocale.c_str());
return result;
}


int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
std::string strTest = "1234中文abcd";
std::wstring destWStr = s2ws(strTest);
return a.exec();
}

执行效果:



上VS源码:

#include <afx.h>
#include <string>
#include <iostream>
using namespace std;

#pragma warning (disable: 4996)

std::string ws2s(const std::wstring &ws)
{
size_t i;
std::string curLocale = setlocale(LC_ALL, NULL);
setlocale(LC_ALL, "");
const wchar_t* _source = ws.c_str();
size_t _dsize = 2 * ws.size() + 1;
char* _dest = new char[_dsize];
memset(_dest, 0x0, _dsize);
wcstombs_s(&i, _dest, _dsize, _source, _dsize);
std::string result = _dest;
delete[] _dest;
setlocale(LC_ALL, curLocale.c_str());
return result;
}

std::wstring s2ws(const std::string &s)
{
size_t i;
std::string curLocale = setlocale(LC_ALL, NULL);
setlocale(LC_ALL, "");
const char* _source = s.c_str();
size_t _dsize = s.size() + 1;
wchar_t* _dest = new wchar_t[_dsize];
wmemset(_dest, 0x0, _dsize);
mbstowcs_s(&i, _dest, _dsize, _source, _dsize);
std::wstring result = _dest;
delete[] _dest;
setlocale(LC_ALL, curLocale.c_str());
return result;
}

int main()
{
std::string strTest = "1234中文abcd";
std::wstring destWStr = s2ws(strTest);
return 0;
}


VS执行结果:

...全文
275 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
sty_app 2021-03-04
  • 打赏
  • 举报
回复
引用 13 楼 真相重于对错 的回复:
[quote=引用 11 楼 sty_app 的回复:][quote=引用 10 楼 真相重于对错 的回复:]qt下直接用qstring处理比较简单


要写一份跨平台跨编译器的代码。 QString不行[/quote]
qstring 是qt亲生的,怎么不行?[/quote], 领导要求,能用标c++的尽量用 标c++实现。字符串转换应该属于很基础的内容了,没必要用QT的QString
真相重于对错 2021-03-03
  • 打赏
  • 举报
回复
引用 11 楼 sty_app 的回复:
[quote=引用 10 楼 真相重于对错 的回复:]qt下直接用qstring处理比较简单
要写一份跨平台跨编译器的代码。 QString不行[/quote] qstring 是qt亲生的,怎么不行?
  • 打赏
  • 举报
回复
引用 9 楼 sty_app 的回复:
[quote=引用 6 楼 早打大打打核战争 的回复:]代码写复杂了,可以简化一些:

#include <codecvt>

std::string ws2s(const std::wstring &ws)
{
std::wstring_convert<std::codecvt_utf16<wchar_t, 0x10fffful,
std::codecvt_mode::little_endian>, wchar_t> cv;

//std::locale::global(std::locale(""));
//setlocale(LC_CTYPE, "");
return cv.to_bytes(ws);
}

std::wstring s2ws(const std::string &s)
{
std::wstring_convert<std::codecvt_utf16<wchar_t, 0x10fffful,
std::codecvt_mode::little_endian>, wchar_t> cv;

//std::locale::global(std::locale(""));
//setlocale(LC_CTYPE, "");
return cv.from_bytes(s);
}

貌似不行哦

[/quote]

程序输出一下看看
sty_app 2021-03-03
  • 打赏
  • 举报
回复
引用 10 楼 真相重于对错 的回复:
qt下直接用qstring处理比较简单


要写一份跨平台跨编译器的代码。 QString不行
真相重于对错 2021-03-01
  • 打赏
  • 举报
回复
qt下直接用qstring处理比较简单
sty_app 2021-03-01
  • 打赏
  • 举报
回复
引用 6 楼 早打大打打核战争 的回复:
代码写复杂了,可以简化一些:

#include <codecvt>

std::string ws2s(const std::wstring &ws)
{
std::wstring_convert<std::codecvt_utf16<wchar_t, 0x10fffful,
std::codecvt_mode::little_endian>, wchar_t> cv;

//std::locale::global(std::locale(""));
//setlocale(LC_CTYPE, "");
return cv.to_bytes(ws);
}

std::wstring s2ws(const std::string &s)
{
std::wstring_convert<std::codecvt_utf16<wchar_t, 0x10fffful,
std::codecvt_mode::little_endian>, wchar_t> cv;

//std::locale::global(std::locale(""));
//setlocale(LC_CTYPE, "");
return cv.from_bytes(s);
}

貌似不行哦

sty_app 2021-03-01
  • 打赏
  • 举报
回复
引用 4 楼 赵4老师 的回复:
[quote=引用 3 楼 sty_app 的回复:][quote=引用 2 楼 芒果黑 的回复:]Qt msvc编译器用你的代码试了没问题 不要在代码直接给中文 从文件读试试看 可能是你Qt Creator配置的问题


我感觉是编码格式的问题, 但在哪里改 不太清除。[/quote]
源代码另存为,选你希望的字符编码,……[/quote]

VScpp文件默认使用的是 GBK, QTcpp文件默认使用的是 UTF-8。
相同源码,运行过程中实际占用的内存并不一致。
sty_app 2021-03-01
  • 打赏
  • 举报
回复
引用 5 楼 丁劲犇 的回复:
如果你的qt用的是VC编译器。用记事本打开你的cpp,另存为gb2312
如果你的qt用的是。mibgw编译器,另存为utf-8


所以说是本地文件的编码格式导致 运行过程中内存提示异常吧。
  • 打赏
  • 举报
回复
代码写复杂了,可以简化一些:

#include <codecvt>

std::string ws2s(const std::wstring &ws)
{
std::wstring_convert<std::codecvt_utf16<wchar_t, 0x10fffful,
std::codecvt_mode::little_endian>, wchar_t> cv;

//std::locale::global(std::locale(""));
//setlocale(LC_CTYPE, "");
return cv.to_bytes(ws);
}

std::wstring s2ws(const std::string &s)
{
std::wstring_convert<std::codecvt_utf16<wchar_t, 0x10fffful,
std::codecvt_mode::little_endian>, wchar_t> cv;

//std::locale::global(std::locale(""));
//setlocale(LC_CTYPE, "");
return cv.from_bytes(s);
}

丁劲犇 2021-02-27
  • 打赏
  • 举报
回复
如果你的qt用的是VC编译器。用记事本打开你的cpp,另存为gb2312 如果你的qt用的是。mibgw编译器,另存为utf-8
芒果黑 2021-02-26
  • 打赏
  • 举报
回复
Qt msvc编译器用你的代码试了没问题 不要在代码直接给中文 从文件读试试看 可能是你Qt Creator配置的问题
赵4老师 2021-02-26
  • 打赏
  • 举报
回复
对电脑而言没有乱码,只有二进制字节;对人脑才有乱码。啊 GBK:0xB0 0xA1,Unicode-16 LE:0x4A 0x55,Unicode-16 BE:0x55 0x4A,UTF-8:0xE5 0x95 0x8A
赵4老师 2021-02-26
  • 打赏
  • 举报
回复
引用 3 楼 sty_app 的回复:
[quote=引用 2 楼 芒果黑 的回复:]Qt msvc编译器用你的代码试了没问题 不要在代码直接给中文 从文件读试试看 可能是你Qt Creator配置的问题
我感觉是编码格式的问题, 但在哪里改 不太清除。[/quote] 源代码另存为,选你希望的字符编码,……
sty_app 2021-02-26
  • 打赏
  • 举报
回复
引用 2 楼 芒果黑 的回复:
Qt msvc编译器用你的代码试了没问题 不要在代码直接给中文 从文件读试试看 可能是你Qt Creator配置的问题


我感觉是编码格式的问题, 但在哪里改 不太清除。

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧