C++中,将.txt文件的编码由UTF-8转换为ANSI
2012-02-29 21:28
549 查看
1,将UTF-8转换为
int Utf8ToAnsi(const char *pstrUTF8, char *pstrAnsi)
{
int i = 0;
int j = 0;
char strUnicode[200] = {0};
i = MultiByteToWideChar(CP_UTF8, 0, pstrUTF8, -1, NULL, 0);
memset(strUnicode, 0, i);
MultiByteToWideChar(CP_UTF8, 0, pstrUTF8, -1, strUnicode, i);
j = WideCharToMultiByte(CP_ACP, 0, strUnicode, -1, NULL, 0, NULL, NULL);
WideCharToMultiByte(CP_ACP, 0, strUnicode, -1, pstrAnsi, j, NULL, NULL);
return 0;
}
2,其他的编码转换
qp::StringW Global::AnsiToUnicode(const char* buf)
{
int len = ::MultiByteToWideChar(CP_ACP, 0, buf, -1, NULL, 0);
if (len == 0) return L"";
std::vector<wchar_t> unicode(len);
::MultiByteToWideChar(CP_ACP, 0, buf, -1, &unicode[0], len);
return &unicode[0];
}
qp::StringA Global::UnicodeToAnsi(const wchar_t* buf)
{
int len = ::WideCharToMultiByte(CP_ACP, 0, buf, -1, NULL, 0, NULL, NULL);
if (len == 0) return "";
std::vector<char> utf8(len);
::WideCharToMultiByte(CP_ACP, 0, buf, -1, &utf8[0], len, NULL, NULL);
return &utf8[0];
}
qp::StringW Global::Utf8ToUnicode(const char* buf)
{
int len = ::MultiByteToWideChar(CP_UTF8, 0, buf, -1, NULL, 0);
if (len == 0) return L"";
std::vector<wchar_t> unicode(len);
::MultiByteToWideChar(CP_UTF8, 0, buf, -1, &unicode[0], len);
return &unicode[0];
}
qp::StringA Global::UnicodeToUtf8(const wchar_t* buf)
{
int len = ::WideCharToMultiByte(CP_UTF8, 0, buf, -1, NULL, 0, NULL, NULL);
if (len == 0) return "";
std::vector<char> utf8(len);
::WideCharToMultiByte(CP_UTF8, 0, buf, -1, &utf8[0], len, NULL, NULL);
return &utf8[0];
}
程序来自http://topic.csdn.net/u/20101008/12/895e5aaa-3db5-4c9f-8d06-ae7b04913132.html。
int Utf8ToAnsi(const char *pstrUTF8, char *pstrAnsi)
{
int i = 0;
int j = 0;
char strUnicode[200] = {0};
i = MultiByteToWideChar(CP_UTF8, 0, pstrUTF8, -1, NULL, 0);
memset(strUnicode, 0, i);
MultiByteToWideChar(CP_UTF8, 0, pstrUTF8, -1, strUnicode, i);
j = WideCharToMultiByte(CP_ACP, 0, strUnicode, -1, NULL, 0, NULL, NULL);
WideCharToMultiByte(CP_ACP, 0, strUnicode, -1, pstrAnsi, j, NULL, NULL);
return 0;
}
2,其他的编码转换
qp::StringW Global::AnsiToUnicode(const char* buf)
{
int len = ::MultiByteToWideChar(CP_ACP, 0, buf, -1, NULL, 0);
if (len == 0) return L"";
std::vector<wchar_t> unicode(len);
::MultiByteToWideChar(CP_ACP, 0, buf, -1, &unicode[0], len);
return &unicode[0];
}
qp::StringA Global::UnicodeToAnsi(const wchar_t* buf)
{
int len = ::WideCharToMultiByte(CP_ACP, 0, buf, -1, NULL, 0, NULL, NULL);
if (len == 0) return "";
std::vector<char> utf8(len);
::WideCharToMultiByte(CP_ACP, 0, buf, -1, &utf8[0], len, NULL, NULL);
return &utf8[0];
}
qp::StringW Global::Utf8ToUnicode(const char* buf)
{
int len = ::MultiByteToWideChar(CP_UTF8, 0, buf, -1, NULL, 0);
if (len == 0) return L"";
std::vector<wchar_t> unicode(len);
::MultiByteToWideChar(CP_UTF8, 0, buf, -1, &unicode[0], len);
return &unicode[0];
}
qp::StringA Global::UnicodeToUtf8(const wchar_t* buf)
{
int len = ::WideCharToMultiByte(CP_UTF8, 0, buf, -1, NULL, 0, NULL, NULL);
if (len == 0) return "";
std::vector<char> utf8(len);
::WideCharToMultiByte(CP_UTF8, 0, buf, -1, &utf8[0], len, NULL, NULL);
return &utf8[0];
}
程序来自http://topic.csdn.net/u/20101008/12/895e5aaa-3db5-4c9f-8d06-ae7b04913132.html。
相关文章推荐
- C++中,将.txt文件的编码由UTF-8转换为ANSI
- Ansi,Unicode(little endian),Unicode big endian,UTF-8编码文件相互转换
- 把ANSI格式的TXT文件批量转换成UTF-8文件类型
- C++文件编码由GBK转换UTF-8的解决方法
- 把ANSI格式的TXT文件批量转换成UTF-8文件类型
- Java转换txt文件编码(GBK转UTF-8)
- unicode, ANSI, utf-8字符集之间的转换 C,C++
- Linux从zh_CN.GB2312迁移到zh_CN.UTF-8 - 包括 文件内容编码转换,文件名编码转换..
- Qt读取ANSI格式文件——利用QTextCodec将其他编码格式转换为Unicode格式
- unicode utf-8 gb2312编码 使用C++相互转换的代码
- ASP+FSO生成的网页文件默认编码格式以及转换成UTF-8编码方法
- 如何在 Linux 中将文件编码转换为 UTF-8
- GBK(GB2312)向UTF-8的编码转换 -- C++
- php自动识别文件编码并转换为UTF-8的方法
- 一行Linux命令查找所有非UTF-8编码的文件,再一行命令,都转换成UTF-8
- .java文件的ANSI编码转换为UTF-8编码
- ANSI、UNICODE、UTF-8编码格式相互之间转换
- 识别常见编码格式文件并转换成UTF-8编码的java实现
- Ubuntu转换文件编码格式(gbk to utf-8 )