在c++支持unicode转换和unicode文件保存
来源:互联网 发布:大赢家炒股软件 编辑:程序博客网 时间:2024/06/05 19:06
/********************************************************************
created: 2008/01/11
created: 11:1:2008 4:30
filename: f:/备份/开发资源/解决方案/others1/prj2/prj2.cpp
file path: f:/备份/开发资源/解决方案/others1/prj2
file base: prj2
file ext: cpp
author: Loserwang
purpose: display the unicode
*********************************************************************/
#include <cstring>
#include <clocale>
#include <iostream>
#include <xlocale>
#include <string>
#include <fstream>
#include <algorithm>
#include <functional>
#include <iomanip>
#ifdef UNICODE
#define unicode_header true
#define _t(x) L##x
#define _convert mbstowcs
#define _tcout wcout
#define _tsetlocale _wsetlocale
typedef wchar_t _tchar;
typedef _tchar _convert_dst;
typedef char _convert_src;
typedef size_t _convert_sz;
#else
#define unicode_header false
#define _t(x) x
#define _convert wcstombs
#define _tcout cout
#define _tsetlocale setlocale
typedef char _tchar;
typedef _tchar _convert_dst;
typedef wchar_t _convert_src;
typedef size_t _convert_sz;
#endif
namespace std{
typedef basic_string<_tchar> _tstring;
};
template<typename DisTy, typename CharTy = _tchar>class disp
:public std::unary_function<void, DisTy>
{
typedef std::basic_ostream<CharTy> ostype;
const _tchar* delim;
ostype& os;
public:
disp(ostype& os_ = std::_tcout, const _tchar* delim_ = _t("/t"))
:os(os_), delim(delim_)
{}
template<typename Ty1> void operator()(const Ty1& t)
{
if(delim)
os << DisTy(t) << delim;
else
os << DisTy(t) ;
}
};
std::_tstring make_tstring(std::string& s, const _tchar* name)
{
if(typeid(_tchar) == typeid(char))
return *reinterpret_cast<std::_tstring*>(&s);
std::auto_ptr<_convert_dst> dst(new _convert_dst[s.size() + 1]);
const _tchar* oname = _tsetlocale(LC_ALL, name);
_convert(dst.get(), (_convert_src*)s.c_str(), s.size() + 1);
_tsetlocale(LC_ALL, oname);
return std::basic_string<_tchar>(dst.get());
}
int main()
{
std::_tcout.imbue(std::locale(".936"));
std::string locale_name = std::_tcout.getloc().name();
std::_tstring s = make_tstring(locale_name, _t(".936"));
std::_tcout << s << std::endl;
locale_name = "一生梦与醒缘份纠缠不清/r/n"
"三世红尘独享尽爱与恨/r/n"
"缘终一个字你我心手相牵/r/n"
"渺渺茫茫只求一生缘/r/n";
s = make_tstring(locale_name, _t(".936"));
std::_tcout << s << std::endl;
char* bytes = (char*)(s.c_str());
//std::for_each(bytes, bytes + (s.size() + 1) * sizeof(_tchar), disp<int>());
std::copy(bytes, bytes + (s.size() + 1) * sizeof(_tchar), std::ostream_iterator<int, _tchar>(std::_tcout, _t("/t")));
std::ofstream f(".//..//outfiles//prj2.txt", std::ios_base::binary);
unicode_header ? f << char(0xFF) << char(0xFE) : void();
//for_each(bytes, bytes + (s.size() + 1) * sizeof(_tchar), disp<char, char>(f, 0));
std::copy(bytes, bytes + (s.size() + 1) * sizeof(_tchar), std::ostream_iterator<char>(f));
f.close();
return 0;
}
- 在c++支持unicode转换和unicode文件保存
- Java 保存 Unicode 文件
- 无法在 unicode 和非 unicode 字符串数据类型之间转换。
- 无法在 unicode 和非 unicode 字符串数据类型之间转换。
- java 中文和unicode字符串互相转换(文件需要保存为gbk2312或者asci格式)
- [C/C++]_[utf8和unicode的相互转换]
- 如何在写文件时转换unicode汉字避免乱码
- unicode工程下,标准库保存unicode字符到文件
- 解决问题:SSIS中,列“column”无法在 unicode和非unicode字符串数据类型之间转换
- CString和string在unicode与非unicode下的相互转换
- vc在unicode和非unicode情况下cstring转换为float
- SSIS无法在unicode和非unicode 字符串数据类型之间转换
- 编码转换(ASCII和Unicode、Unicode和中文相互转换)
- 用C函数来转换Unicode和ANSI文字
- C语言实现Unicode和UTF8之间的转换
- .Net(C#)汉字和Unicode编码互相转换
- Utf8和Unicode转换问题 C语言 千字文问题
- Symbian OS上的文件操作和UNICODE转换
- 不要“拖拖拉拉”
- java preference OP zhuanzai勇敢的心
- SQLSERVER备份和对日志的处理
- 字符编码
- 有关UNICODE、ANSI字符集和相关字符串操作
- 在c++支持unicode转换和unicode文件保存
- 常用字符集编码名称和代码对应关系表
- StringBuilder的效率——连接10万个单字符
- SQL 小写转大写金额
- VS2005数据库操作示例
- HP大中华区总裁孙振耀退休感言
- 关于SQL语句的自动生成!(一)
- 在ASP.NET中重写URL
- 使用javascript模拟了一个类似Java的HashMap类