MultiByteToWideChar使用的问题

zifeng0923_2008 2009-06-02 11:20:03

ProFileName msgfil;
char *pszAnsi = "proesignbycoordinate.txt";
MultiByteToWideChar(CP_ACP, 0, pszAnsi, -1, msgfil, sizeof (msgfil)),在使用的过程中 ,进行转化后msgfil[0],msgfil[1]中显示乱码,为什么存放到msgfil中的位置从msgfil[2]开始呢?弄不清楚 ,请教各位了
...全文
108 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
zifeng0923_2008 2009-06-11
  • 打赏
  • 举报
回复
搞定 ,结贴
老邓 2009-06-02
  • 打赏
  • 举报
回复
MultiByteToWideChar(CP_ACP, 0, buf, -1, &unicode[0], len);
这是根据前面获取的len,来真正转换编码。
老邓 2009-06-02
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 zifeng0923_2008 的回复:]
那我设定的len 为固定值 ,还是出现原来的错误
[/Quote]
你用固定值当然不对了。
老邓 2009-06-02
  • 打赏
  • 举报
回复
int len = MultiByteToWideChar(CP_ACP, 0, buf, -1, NULL, 0);
if (len == 0) return shared_ptr<wstring>(new wstring(L""));
这是获取转换后的字符串长度的,不是你现在字符串长度!
zifeng0923_2008 2009-06-02
  • 打赏
  • 举报
回复
那我设定的len 为固定值 ,还是出现原来的错误
SoRoMan 2009-06-02
  • 打赏
  • 举报
回复
MultiByteToWideChar(CP_ACP, 0, pszAnsi, -1, msgfil, sizeof (msgfil))
sizeof (msgfil)这个参数不对,应该是字符串长度。
老邓 2009-06-02
  • 打赏
  • 举报
回复
你要先取得转换后的buf长度。
给你参考一下我的工程里的函数:


shared_ptr<wstring> MB2WC(const char* buf)
{
int len = MultiByteToWideChar(CP_ACP, 0, buf, -1, NULL, 0);
if (len == 0) return shared_ptr<wstring>(new wstring(L""));

vector<wchar_t> unicode(len);
MultiByteToWideChar(CP_ACP, 0, buf, -1, &unicode[0], len);

return shared_ptr<wstring>(new wstring(&unicode[0]));
}


注意:-1
老邓 2009-06-02
  • 打赏
  • 举报
回复
你要先取得转换后的buf长度。
给你参考一下我的工程里的函数:


shared_ptr<wstring> MB2WC(const char* buf)
{
int len = MultiByteToWideChar(CP_ACP, 0, buf, -1, NULL, 0);
if (len == 0) return shared_ptr<wstring>(new wstring(L""));

vector<wchar_t> unicode(len);
MultiByteToWideChar(CP_ACP, 0, buf, -1, &unicode[0], len);

return shared_ptr<wstring>(new wstring(&unicode[0]));
}


注意:-1
老邓 2009-06-02
  • 打赏
  • 举报
回复
你要先取得转换后的buf长度。
给你参考一下我的工程里的函数:


shared_ptr<wstring> MB2WC(const char* buf)
{
int len = MultiByteToWideChar(CP_ACP, 0, buf, -1, NULL, 0);
if (len == 0) return shared_ptr<wstring>(new wstring(L""));

vector<wchar_t> unicode(len);
MultiByteToWideChar(CP_ACP, 0, buf, -1, &unicode[0], len);

return shared_ptr<wstring>(new wstring(&unicode[0]));
}


注意:-1
老邓 2009-06-02
  • 打赏
  • 举报
回复
ProFileName msgfil;

要改成:
wchar_t msgfile[1024]
老邓 2009-06-02
  • 打赏
  • 举报
回复
#include <windows.h>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

wstring MB2WC(const char* buf)
{
int len = MultiByteToWideChar(CP_ACP, 0, buf, -1, NULL, 0);
if (len == 0) return wstring(L"");

vector<wchar_t> unicode(len);
MultiByteToWideChar(CP_ACP, 0, buf, -1, &unicode[0], len);

return wstring(&unicode[0]);
}

int main()
{
char *pszAnsi = "proesignbycoordinate.txt";
wstring ws = MB2WC(pszAnsi);
wcout << ws << endl << (ws.length() == wcslen(ws.c_str())) << endl;
return 0;
}

输出Unicode字符串:
proesignbycoordinate.txt
1
老邓 2009-06-02
  • 打赏
  • 举报
回复
ProFileName msgfil;
这是什么类型?自定义的类?
用char buf[]
zifeng0923_2008 2009-06-02
  • 打赏
  • 举报
回复
int err=MultiByteToWideChar(CP_ACP,0,str,-1,wmsgfil,len); 中不是 wmsgfil ,写错了 是 msgfil
zifeng0923_2008 2009-06-02
  • 打赏
  • 举报
回复
ProFileName msgfil;
char *pszAnsi = "proesignbycoordinate.txt";
int len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
int err=MultiByteToWideChar(CP_ACP,0,str,-1,wmsgfil,len);
我现在改成这样了 ,还是出现乱码 ,哎 ,到底问题出现在哪里呢 ?

64,676

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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