如何将UTF-8编码的文本文件转换成ANSI编码的文件?

heartgoon2010 2010-05-24 10:54:36
RT。请不要用另存,因为我有一大堆UTF-8编码的文本文件要转换。
最好能给个例子代码,先谢各位了!
...全文
1195 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
纠结的程序猿 2010-05-25
  • 打赏
  • 举报
回复
上面这个软件是免费的
linxinshi16 2010-05-24
  • 打赏
  • 举报
回复
Window C++编程加群110126733
linxinshi16 2010-05-24
  • 打赏
  • 举报
回复
int utf8towc(LPCSTR pcUtf8, UINT uUtf8Size, LPWSTR pwc, UINT uWideCharSize)
{
LPWSTR pwc0 = pwc;

while (uUtf8Size && uWideCharSize)
{
BYTE ucChar = *pcUtf8++;
if (ucChar < 0x80)
{
uUtf8Size--;
uWideCharSize--;
*(pwc++) = ucChar;
}
else if ((ucChar & 0xC0) != 0xC0)
{
return -1; // Invalid UTF8 string..
}
else
{
BYTE ucMask = 0xE0;
UINT uExpectedBytes = 1;
while ((ucChar & ucMask) == ucMask)
{
ucMask |= ucMask >> 1;
if (++uExpectedBytes > 3)
return -1; // Invalid UTF8 string..
}

if (uUtf8Size <= uExpectedBytes)
return -1; // Invalid UTF8 string..

UINT uProcessedBytes = 1 + uExpectedBytes;
UINT uWideChar = (UINT)(ucChar & ~ucMask);
if (uExpectedBytes == 1)
{
if ((uWideChar & 0x1E) == 0)
return -1; // Invalid UTF8 string..
}
else
{
if (uWideChar == 0 && ((BYTE)*pcUtf8 & 0x3F & (ucMask << 1)) == 0)
return -1; // Invalid UTF8 string..

if (uExpectedBytes == 2)
{
//if (uWideChar == 0x0D && ((BYTE)*pcUtf8 & 0x20))
// return -1;
}
else if (uExpectedBytes == 3)
{
if (uWideChar > 4)
return -1; // Invalid UTF8 string..
if (uWideChar == 4 && ((BYTE)*pcUtf8 & 0x30))
return -1; // Invalid UTF8 string..
}
}

if (uWideCharSize < (UINT)(uExpectedBytes > 2) + 1)
break; // buffer full

while (uExpectedBytes--)
{
if (((ucChar = (BYTE)*(pcUtf8++)) & 0xC0) != 0x80)
return -1; // Invalid UTF8 string..
uWideChar <<= 6;
uWideChar |= (ucChar & 0x3F);
}
uUtf8Size -= uProcessedBytes;

if (uWideChar < 0x10000)
{
uWideCharSize--;
*(pwc++) = (WCHAR)uWideChar;
}
else
{
uWideCharSize -= 2;
uWideChar -= 0x10000;
*(pwc++) = (WCHAR)(0xD800 | (uWideChar >> 10));
*(pwc++) = (WCHAR)(0xDC00 | (uWideChar & 0x03FF));
}
}
}

return pwc - pwc0;
}
电驴中有个源码这么写的,参考一下
白云飘飘飘 2010-05-24
  • 打赏
  • 举报
回复
int main(void)
{

string str="abc";//utf8
string dst;//ansi
wstring wstr;
int len = MultiByteToWideChar(CP_UTF8,0,str.c_str(),str.size(),NULL,0);
wstr.resize(len);
MultiByteToWideChar(CP_UTF8,0,str.c_str(),str.size(),&wstr[0],len);
len=WideCharToMultiByte(CP_ACP,0,wstr.c_str(),wstr.size(),NULL,0,NULL,NULL);
dst.resize(len);
WideCharToMultiByte(CP_ACP,0,wstr.c_str(),wstr.size(),&dst[0],len,NULL,NULL);
cout<<dst<<endl;
system("pause");
return 0;
}
赵4老师 2010-05-24
  • 打赏
  • 举报
回复
用system("ConvertZ.exe /i:utf8 /o:gbk utf8.txt gbk.txt");
heartgoon2010 2010-05-24
  • 打赏
  • 举报
回复
to freezezdj
UE是什么啊,能具体点吗?
heartgoon2010 2010-05-24
  • 打赏
  • 举报
回复
to classpatterns
不会用Qt啊,这位朋友能给个例子程序吗?
heartgoon2010 2010-05-24
  • 打赏
  • 举报
回复
to lhcwjy
不好意思啊,不太会用MultiByteToWideChar函数,所以先试着写了一个样例程序,但是总有错误,希望这位朋友能帮忙检查一下:
#include<iostream>
#include<fstream>
#include<string>
#include<winnls.h>

using std::fstream;
using std::cout;
using std::endl;
using std::string;

int main()
{
std::fstream infile("f:\\sample.txt",fstream::out);
//f:\\sample.txt文件时UTF-8编码格式的
string str;
int count=1;
std::getline(infile,str);
int len = MultiByteToWideChar(CP_UTF8 , 0 , str.c_str() , -1 , NULL , 0);
cout << len <<endl;
return 0;
}
冻结 2010-05-24
  • 打赏
  • 举报
回复
我觉得用UE就很方便呀。
FingerStyle 2010-05-24
  • 打赏
  • 举报
回复
用Qt, QTextCode QString 很简单搞定。
白云飘飘飘 2010-05-24
  • 打赏
  • 举报
回复
先用MultibyteToWideChar转换成UTF-16,再用WideCharToMultibyte转换成ANSI
heartgoon2010 2010-05-24
  • 打赏
  • 举报
回复
怎么就没人回复呢,大家快来看看啊

64,282

社区成员

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

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