unicode转utf_8失败

tan625747 2011-11-01 04:31:42
#include <Windows.h>
#include <iostream>
#include <vector>
#include <bitset>
#include <stdio.h>
#include <memory.h>
#include "ChineseCode.h"
using namespace std;

std::wstring UT2WC(const char* buf)
{
int len = MultiByteToWideChar(CP_UTF8, 0, buf, -1, NULL, 0);
std::vector<wchar_t> unicode(len);
MultiByteToWideChar(CP_UTF8, 0, buf, -1, &unicode[0], len);

return std::wstring(&unicode[0]);
}

//UNICODE转换成UTF-8
std::string WC2UT(wchar_t* buf)
{
int len = WideCharToMultiByte(CP_UTF8, 0, buf, -1, NULL, 0, NULL, NULL);
std::vector<char> utf8(len);
WideCharToMultiByte(CP_UTF8, 0, buf, -1, &utf8[0], len, NULL, NULL);

return std::string(&utf8[0]);
}

std::wstring MB2WC(const char* buf)
{
int len = MultiByteToWideChar(CP_ACP, 0, buf, -1, NULL, 0);
std::vector<wchar_t> unicode(len);
MultiByteToWideChar(CP_ACP, 0, buf, -1, &unicode[0], len);

return std::wstring(&unicode[0]);
}

std::string WC2MB(const wchar_t* buf)
{
int len = WideCharToMultiByte(CP_ACP, 0, buf, -1, NULL, 0, NULL, NULL);
std::vector<char> utf8(len);
WideCharToMultiByte(CP_ACP, 0, buf, -1, &utf8[0], len, NULL, NULL);

return std::string(&utf8[0]);
}

int main()
{
setlocale(LC_ALL, "");

wchar_t* s1 = L"UNICODE转换成UTF-8";
char out[512]="";
cout<<WC2UT(sl).c_str()<<endl;

const char* s2 = "ANSI转换成UNICODE";
wcout << MB2WC(s2).c_str() << endl;

const wchar_t* s3 = L"UNICODE转换成ANSI";
cout << WC2MB(s3).c_str() << endl;

return 0;
}


其中 UNICODE转换成UTF-8 函数没转成功


std::string WC2UT(wchar_t* buf)
{
int len = WideCharToMultiByte(CP_UTF8, 0, buf, -1, NULL, 0, NULL, NULL);
std::vector<char> utf8(len);
WideCharToMultiByte(CP_UTF8, 0, buf, -1, &utf8[0], len, NULL, NULL);

return std::string(&utf8[0]);
}



请大家帮忙完善,提供给大家使用,谢谢大家了。
...全文
112 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
tan625747 2011-11-01
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 jixingzhong 的回复:]

查看的时候,是否把显示的工具调整为相应的编码方式了啊。
[/Quote]
调试的方式查看
jixingzhong 2011-11-01
  • 打赏
  • 举报
回复
查看的时候,是否把显示的工具调整为相应的编码方式了啊。
tan625747 2011-11-01
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 loaden 的回复:]

嘿嘿,这像我写的代码。
怎么个不成功法?
[/Quote]

就是你的呀,好像这个还是乱码
老邓 2011-11-01
  • 打赏
  • 举报
回复
嘿嘿,这像我写的代码。
怎么个不成功法?
赵4老师 2011-11-01
  • 打赏
  • 举报
回复
std::vector<char> 和char[]不是一回事。

64,688

社区成员

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

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