惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Forbes - Security
Forbes - Security
GbyAI
GbyAI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
SegmentFault 最新的问题
Y
Y Combinator Blog
Recorded Future
Recorded Future
博客园 - Franky
I
InfoQ
T
The Blog of Author Tim Ferriss
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
T
Tailwind CSS Blog
Cyberwarzone
Cyberwarzone
The Register - Security
The Register - Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
雷峰网
雷峰网
P
Palo Alto Networks Blog
G
GRAHAM CLULEY
Cloudbric
Cloudbric
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
MongoDB | Blog
MongoDB | Blog
F
Full Disclosure
Google DeepMind News
Google DeepMind News
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
Check Point Blog
爱范儿
爱范儿
The GitHub Blog
The GitHub Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
T
Threat Research - Cisco Blogs
U
Unit 42
N
Netflix TechBlog - Medium
The Cloudflare Blog
Spread Privacy
Spread Privacy
Microsoft Azure Blog
Microsoft Azure Blog
美团技术团队
T
Troy Hunt's Blog
Engineering at Meta
Engineering at Meta
H
Heimdal Security Blog
TaoSecurity Blog
TaoSecurity Blog
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tenable Blog
B
Blog
S
Securelist
H
Hacker News: Front Page
Google Online Security Blog
Google Online Security Blog
G
Google Developers Blog

博客园 - format

使用Arduino Nano驱动Lora模块 DIY一个物联网平台:想法 jQuery 批量操作checkbox 国内的maven镜像 Python Flask UnicodeDecodeError 编码错误解决 关于Python的web框架 NHibernate 3 中的 In 集合查询 在MVC 4 中使用自定义Membership [学习][Java] Struts2 与页面传值 Linux学习 -- Linux的可执行文件 WIN32简单的窗体代码,带函数功能注释 WINSocket编程 发生HTTP GET请求,并接收服务器返回 Win32 API 的文件操作 - format C++ 类中封装Win32API的回调函数 Win32编程 创建从资源文件定义的对话框 Oracle 数据库 用脚本建表空间 把UserControl通过代码控制输入HTML WebService调用时候的Object Moved 异常 ICTCLAS 平台调用的封装
(转) C++ Utf8字符转换Gb312编码,解决TinyXml中文乱码
format · 2010-09-21 · via 博客园 - format

  1 #ifndef TRANSFORM
  2 #define TRANSFORM
  3 #include <map>
  4 using namespace std;
  5 // 是否启用map转换,建议启用
  6 // 使用map转换的话,同一个常量字符串不会做两次转换
  7 #define TEST_TYPE_MAP
  8 
  9 typedef map<const char*const char*> strmap;
 10 
 11 class CUtf8String
 12 {
 13     public:
 14         inline CUtf8String(const char* gb2312)
 15         {
 16             m_bIsConst = true;
 17 #ifdef TEST_TYPE_MAP
 18             if (m[gb2312])
 19             {
 20                 m_utf8 = m[gb2312];
 21                 return ;
 22             }
 23 #endif
 24             int buffLen = 0;
 25             WCHAR wbuff[5120];
 26             MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wbuff, 5120);
 27             buffLen = WideCharToMultiByte(CP_UTF8, 0, wbuff, -1, NULL, 000);
 28             m_utf8 = new char[buffLen+1];
 29             WideCharToMultiByte(CP_UTF8, 0, wbuff, -1, (LPSTR)m_utf8, buffLen, 00);
 30 #ifdef TEST_TYPE_MAP
 31             m[gb2312] = m_utf8;
 32 #endif
 33         }
 34 
 35         inline CUtf8String(char* gb2312)
 36         {
 37             m_bIsConst = false;
 38             int buffLen = 0;
 39             WCHAR wbuff[5120];
 40             MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wbuff, 5120);
 41             buffLen = WideCharToMultiByte(CP_UTF8, 0, wbuff, -1, NULL, 000);
 42             m_utf8 = new char[buffLen+1];
 43             WideCharToMultiByte(CP_UTF8, 0, wbuff, -1, (LPSTR)m_utf8, buffLen, 00);
 44         }
 45 
 46         inline ~CUtf8String()
 47             {
 48 #ifndef TEST_TYPE_MAP
 49             if (m_utf8)
 50             {
 51                 delete m_utf8;
 52                 m_utf8 = 0;
 53             }
 54 #else
 55             if (!m_bIsConst)
 56                 {
 57                 if (m_utf8)
 58                     {
 59                     delete m_utf8;
 60                     m_utf8 = 0;
 61                     }
 62                 }
 63 #endif
 64             }
 65 
 66         inline operator char*()
 67             {
 68             return (char*)m_utf8;
 69             }
 70     private:
 71         const char* m_utf8;
 72         bool m_bIsConst;
 73 #ifdef TEST_TYPE_MAP
 74         static strmap m;
 75 #endif
 76 };
 77 
 78 class CGb2312String
 79     {
 80     public:
 81         inline CGb2312String(const char* utf8)
 82             {
 83 #ifdef TEST_TYPE_MAP
 84             if (m[utf8])
 85             {
 86                 m_gb2312 = 0;
 87                 m_gb2312 = m[utf8];
 88             }
 89 #endif
 90             int buffLen = 0;
 91             WCHAR wbuff[5120];
 92             MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wbuff, 5120);
 93             buffLen = WideCharToMultiByte(CP_ACP, 0, wbuff, -1, NULL, 000);
 94             m_gb2312 = new char[buffLen+1];
 95             WideCharToMultiByte(CP_ACP, 0, wbuff, -1, (LPSTR)m_gb2312, buffLen, 00);
 96 #ifdef TEST_TYPE_MAP
 97             m[utf8] = m_gb2312;
 98 #endif
 99         }
100 
101         inline CGb2312String(char* utf8)
102         {
103 #ifdef TEST_TYPE_MAP
104             if (m[utf8])
105             {
106                 m_gb2312 = 0;
107                 m_gb2312 = m[utf8];
108             }
109 #endif
110             int buffLen = 0;
111             WCHAR wbuff[5120];
112             MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wbuff, 5120);
113             buffLen = WideCharToMultiByte(CP_ACP, 0, wbuff, -1, NULL, 000);
114             m_gb2312 = new char[buffLen+1];
115             WideCharToMultiByte(CP_ACP, 0, wbuff, -1, (LPSTR)m_gb2312, buffLen, 00);
116 #ifdef TEST_TYPE_MAP
117             m[utf8] = m_gb2312;
118 #endif
119         }
120 
121         inline ~CGb2312String()
122         {
123 #ifndef TEST_TYPE_MAP
124             if (m_gb2312)
125                 {
126                 delete m_gb2312;
127                 m_gb2312 = 0;
128                 }
129 #endif
130         }
131 
132         inline operator char*()
133         {
134             return (char*)m_gb2312;
135         }
136     private:
137         const char* m_gb2312;
138 #ifdef TEST_TYPE_MAP
139         static strmap m;
140 #endif
141 };
142 
143 #ifdef TEST_TYPE_MAP
144 strmap CUtf8String::m;
145 strmap CGb2312String::m;
146 #endif
147 #endif
148 
149 
150 //===================无聊的分割线==========================
151 
152 #define U   (CUtf8String)
153 
154 #define W   (CGb2312String)
155 
156 // 使用方法
157 int main(int argc, char* argv[])
158 {
159     // 打印出乱码即为UTF8的编码,方便吧。C++还是确实很强悍的
160     printf("%s", U"你好中国!");
161 }