1. /******Encoding.h*******/
  2. #include "Poco/UnicodeConverter.h"
  3. #include "Poco/Exception.h"
  4. #include "Poco/DigestEngine.h"
  5.  
  6. #define MyLib_API Foundation_API
  7.  
  8. using namespace Poco;
  9.  
  10. POCO_DECLARE_EXCEPTION(MyLib_API, EncodeException, Exception)
  11.  
  12. class Encoding
  13. {
  14. public:
  15. enum ByteOrderType
  16. {
  17. BIG_ENDIAN_BYTE_ORDER,
  18. LITTLE_ENDIAN_BYTE_ORDER,
  19. UNKNOW
  20. };
  21.  
  22. static void GBKToUTF16(const std::string& gbkString, std::wstring& utf16String) throw(EncodeException);
  23. static void UTF16ToGBK(const std::wstring& utf16String, std::string& gbkString) throw(EncodeException);
  24. static void UTF8ToUTF16(const std::string& utf8String, std::wstring& utf16String) throw(EncodeException);
  25. static void UTF16ToUTF8(const std::wstring& utf16String, std::string& utf8String) throw(EncodeException);
  26. static void UTF8ToGBK(const std::string& utf8String, std::string& gbkString) throw(EncodeException);
  27. static void GBKToUTF8(const std::string& gbkString, std::string& utf8String) throw(EncodeException);
  28. static void EncodeHexString(const std::string& bytes, std::string& hexString);
  29. static void DecodeHexString(const std::string& hexString, std::string& bytes);
  30. static void EncodeHexString(const std::wstring& bytes, std::string& hexString);
  31. static void DecodeHexString(const std::string& hexString, std::wstring& bytes);
  32. static ByteOrderType GetCurrentByteOrder();
  33.  
  34. private:
  35. static Poco::UnicodeConverter _unicodeConverter;
  36. static ByteOrderType _currentByteOrder;
  37. };
  1. /********Encoding.cpp********/#include "Encoding.h"
  2. #include "Poco/NumberParser.h"
  3.  
  4. Poco::UnicodeConverter Encoding::_unicodeConverter;
  5. Encoding::ByteOrderType Encoding::_currentByteOrder;
  6.  
  7. POCO_IMPLEMENT_EXCEPTION(EncodeException, Poco::Exception, "Encoding error")
  8.  
  9. void Encoding::GBKToUTF16(const std::string& gbkString, std::wstring& utf16String)
  10. {
  11. //获得需要分配的空间大小
  12. int size = MultiByteToWideChar(, , gbkString.c_str(), -, NULL, );
  13. std::vector<wchar_t> buff(size);
  14. if(MultiByteToWideChar(, , gbkString.c_str(), -, buff.data(), size) == )
  15. {
  16. //throw a exception
  17. throw EncodeException("GBK convert to UTF16 failed", GetLastError());
  18. }
  19. if(!utf16String.empty())
  20. utf16String.clear();
  21. utf16String.append(buff.data(), buff.size());
  22. }
  23.  
  24. void Encoding::UTF16ToGBK(const std::wstring& utf16String, std::string& gbkString)
  25. {
  26. int size = ;
  27.  
  28. //获得需要分配的空间大小
  29. size = WideCharToMultiByte(, , utf16String.c_str(), -, NULL, , NULL, NULL);
  30. std::vector<char> buff(size);
  31. if(WideCharToMultiByte(, , utf16String.c_str(), -, buff.data(), size, NULL, NULL) == )
  32. throw EncodeException("UTF16 convert to GBK failed", GetLastError());
  33.  
  34. if(!gbkString.empty())
  35. gbkString.clear();
  36. gbkString.append(buff.data(), buff.size());
  37. }
  38.  
  39. void Encoding::UTF8ToUTF16(const std::string& utf8String, std::wstring& utf16String)
  40. {
  41. std::string errorMessage;
  42.  
  43. try
  44. {
  45. _unicodeConverter.toUTF16(utf8String, utf16String);
  46. }
  47. catch(Poco::Exception &e)
  48. {
  49. errorMessage.append("UTF8 convert to UTF16 failed, ");
  50. errorMessage.append(e.message());
  51. }
  52. if(!errorMessage.empty())
  53. throw EncodeException(errorMessage, GetLastError());
  54. }
  55.  
  56. void Encoding::UTF16ToUTF8(const std::wstring& utf16String, std::string& utf8String)
  57. {
  58. std::string errorMessage;
  59.  
  60. try
  61. {
  62. _unicodeConverter.toUTF8(utf16String, utf8String);
  63. }
  64. catch(Poco::Exception &e)
  65. {
  66. errorMessage.append("UTF8 convert to UTF16 failed, ");
  67. errorMessage.append(e.message());
  68. }
  69. if(!errorMessage.empty())
  70. throw EncodeException(errorMessage, GetLastError());
  71. }
  72.  
  73. void Encoding::UTF8ToGBK(const std::string& utf8String, std::string& gbkString)
  74. {
  75. std::wstring utf16String;
  76. std::string errorMessage;
  77.  
  78. try
  79. {
  80. _unicodeConverter.toUTF16(utf8String, utf16String);
  81. UTF16ToGBK(utf16String, gbkString);
  82. }
  83. catch(EncodeException)
  84. {
  85. errorMessage = "UTF8 convert to GBK failed";
  86. }
  87. catch(Poco::Exception &e)
  88. {
  89. errorMessage.append("UTF8 convert to UTF16 failed, ");
  90. errorMessage.append(e.message());
  91. }
  92. if(!errorMessage.empty())
  93. throw EncodeException(errorMessage, GetLastError());
  94. }
  95.  
  96. void Encoding::GBKToUTF8(const std::string& gbkString, std::string& utf8String)
  97. {
  98. std::wstring utf16String;
  99. std::string errorMessage;
  100.  
  101. try
  102. {
  103. GBKToUTF16(gbkString, utf16String);
  104. _unicodeConverter.toUTF8(utf16String, utf8String);
  105. }
  106. catch(EncodeException)
  107. {
  108. errorMessage = "GBK convert to UTF8 failed";
  109. }
  110. catch(Poco::Exception &e)
  111. {
  112. errorMessage.append("UTF8 convert to UTF16 failed, ");
  113. errorMessage.append(e.message());
  114. }
  115. if(!errorMessage.empty())
  116. throw EncodeException(errorMessage, GetLastError());
  117. }
  118.  
  119. void Encoding::EncodeHexString(const std::string& bytes, std::string& hexString)
  120. {
  121. if(!hexString.empty())
  122. hexString.clear();
  123.  
  124. Poco::DigestEngine::Digest digest(bytes.begin(), bytes.end());
  125. hexString = Poco::DigestEngine::digestToHex(digest);
  126. }
  127.  
  128. void Encoding::DecodeHexString(const std::string& hexString, std::string& bytes)
  129. {
  130. unsigned int _value;
  131. if(!bytes.empty())
  132. bytes.clear();
  133.  
  134. for(std::string::size_type i = , j = ; i < hexString.length(); i+=)
  135. {
  136. if(NumberParser::tryParseHex(hexString.substr(i, ), _value))
  137. bytes.push_back(_value);
  138. }
  139. }
  140.  
  141. void Encoding::EncodeHexString(const std::wstring& utf16String, std::string& hexString)
  142. {
  143. if(!hexString.empty())
  144. hexString.clear();
  145.  
  146. Poco::DigestEngine::Digest digest;
  147. for(auto iter = utf16String.begin(); iter != utf16String.end(); ++iter)
  148. {
  149. const unsigned char* ptr = (const unsigned char*)&*iter;
  150. if(GetCurrentByteOrder() == BIG_ENDIAN_BYTE_ORDER)
  151. {
  152. digest.push_back(*ptr);
  153. digest.push_back(*(ptr + ));
  154. }
  155. else if(GetCurrentByteOrder() == LITTLE_ENDIAN_BYTE_ORDER)
  156. {
  157. digest.push_back(*(ptr + ));
  158. digest.push_back(*ptr);
  159. }
  160. else
  161. return;
  162. }
  163.  
  164. hexString = Poco::DigestEngine::digestToHex(digest);
  165. }
  166.  
  167. void Encoding::DecodeHexString(const std::string& hexString, std::wstring& utf16String)
  168. {
  169. unsigned int _value;
  170. if(!utf16String.empty())
  171. utf16String.clear();
  172.  
  173. for(std::string::size_type i = , j = ; i < hexString.length(); i+=)
  174. {
  175. if(NumberParser::tryParseHex(hexString.substr(i, ), _value))
  176. utf16String.push_back(_value);
  177. }
  178. }
  179.  
  180. Encoding::ByteOrderType Encoding::GetCurrentByteOrder()
  181. {
  182. static bool flag = false;
  183. if(flag)
  184. return _currentByteOrder;
  185.  
  186. union
  187. {
  188. char16_t s;
  189. char c[];
  190. }un;
  191.  
  192. un.s = 0x0102;
  193. if(un.c[] == && un.c[] == )
  194. _currentByteOrder = BIG_ENDIAN_BYTE_ORDER;
  195. else if(un.c[] == && un.c[] == )
  196. _currentByteOrder = LITTLE_ENDIAN_BYTE_ORDER;
  197. else
  198. _currentByteOrder = UNKNOW;
  199.  
  200. flag = true;
  201. return _currentByteOrder;
  202. }

基于Poco的UTF8、UTF16、GBK、Hex之间的转换的更多相关文章

  1. UTF-8和GBK编码之间的区别(页面编码、数据库编码区别)以及在实际项目中的应用

    第一节:UTF-8和GBK编码概述 UTF-8 (8-bit Unicode Transformation Format) 是一种针对Unicode的可变长度字符编码,又称万国码,它包含全世界所有国家 ...

  2. ASC与HEX之间的转换

    ASC与HEX之间的转换 有这么两个函数: 函数 原型 功能 返回值 参数 备注 hex2asc __int16 hex2asc(unsigned char *strhex,unsigned char ...

  3. C#中的Byte,String,Int,Hex之间的转换函数。

    /// <summary> Convert a string of hex digits (ex: E4 CA B2) to a byte array. </summary> ...

  4. ascii、unicode、utf-8、gbk区别及转换

    一.编码 ascii: A:00000010 8位 一个字节 unicode: A:00000000 00000001 00000010 00000100 32位 四个字节 中:00000000 00 ...

  5. ascii、unicode、utf-8、gbk 区别?

    发展史: https://www.cnblogs.com/houxt/p/11250878.html python2内容进行编码(默认ascii),而python3对内容进行编码的默认为utf-8. ...

  6. UTF8,UTF16,UTF32,UTF16-LE,UTF16-BE,GBK 之间的转换

    Unicode是Unicode.org制定的编码标准,目前得到了绝大部分操作系统和编程语言的支持.Unicode.org官方对Unicode的定义是:Unicode provides a unique ...

  7. 字符编码之间的相互转换 UTF8与GBK(转载)

    转载自http://www.cnblogs.com/azraelly/archive/2012/06/21/2558360.html UTF8与GBK字符编码之间的相互转换 C++ UTF8编码转换 ...

  8. 【miscellaneous】【C/C++语言】UTF8与GBK字符编码之间的相互转换

    UTF8与GBK字符编码之间的相互转换 C++ UTF8编码转换 CChineseCode 一 预备知识 1,字符:字符是抽象的最小文本单位.它没有固定的形状(可能是一个字形),而且没有值." ...

  9. UNICODE与UTF8和GBK之间的关系

    http://wenku.baidu.com/link?url=bheGEzfSjEx-QX-ciME5oKooKYE08_NJZ02l2kKFa7kVZJ4t8Ks2uSNByovgP2QL6btq ...

随机推荐

  1. spring中AOP

    1 AOP 的功能是把横切的问题(如性能监视.事务管理)模块化.AOP的核心是连接点模型,他提供在哪里发生横切. Spring AOP 的底层是通过使用 JDK 或 CGLib 动态代理技术为目标 b ...

  2. cocos2dx实现3d拾取注意事项

    用的是cocos2dx 3.x,如果是真机测试,glview = cocos2d::GLViewImpl::createWithRect(...)和glview->setDesignResolu ...

  3. 【转】input file accept属性可以限制的文件类型

    来源:http://blog.sina.com.cn/s/blog_6c9d65a10101a8yh.html 在上传文件的时候,需要限制指定的文件类型. <input type="f ...

  4. 跟我一起学习VIM - vim插件合集

    2016-06-14 15:04 13333人阅读 评论(0) 收藏 举报 分类: Linux(104)  目录(?)[+]  前两天同事让我在小组内部分享一下VIM,于是我花了一点时间写了个简短的教 ...

  5. activiti自己定义流程之Spring整合activiti-modeler实例(一):环境搭建

    项目中须要整合activiti-modeler自己定义流程,找了非常多资料后,最终成功的跳转到activiti-modeler流程设计界面.下面是记录: 一.整合基础:eclipse4.4.1.tom ...

  6. c# 封装的7zip压缩 (全源码,不含任何类库)

    1,从soureforge下载sdk(包括汇编,c,c++,c#,java) 下载地址http://nchc.dl.sourceforge.net/project/sevenzip/LZMA%20SD ...

  7. vue轮播图插件vue-awesome-swiper的使用与组件化

    不管是APP还是移动端网页开发,轮播图在大部分项目当中都是存在的,这时候如果用vue开发项目,选择一款好的插件并且封装好是很重要的 1. 推荐使用vue-awesome-swiper 安装:cnpm ...

  8. NIS & Kerberos配置

    NIS & Kerberos配置 所需RPM包列表: krb5-server-1.10.3-42.el6.x86_64.rpm krb5-workstation-1.10.3-42.el6.x ...

  9. 【转载】用Pwnage + Redsnow 制作完美越狱固件

    [转载] 现在貌似IOS 7.X系 大行其道,就算不是IOS7.X ,很多人也装着IOS 6.X系. 进入正文前首先介绍一下自己的装备 设备:iphone4 GSM 大陆行货 (内部版本3,1) 为什 ...

  10. 一款基于TweenMax.js的网页幻灯片

    之前介绍了好多网页幻灯片.今天给大家带来一款基于TweenMax.js的网页幻灯片.这款幻灯片以不规则的碎片百叶窗的形式切换.切换效果非常漂亮.一起看下效果图: 在线预览   源码下载 实现的代码. ...