/******Encoding.h*******/
#include "Poco/UnicodeConverter.h"
#include "Poco/Exception.h"
#include "Poco/DigestEngine.h" #define MyLib_API Foundation_API using namespace Poco; POCO_DECLARE_EXCEPTION(MyLib_API, EncodeException, Exception) class Encoding
{
public:
enum ByteOrderType
{
BIG_ENDIAN_BYTE_ORDER,
LITTLE_ENDIAN_BYTE_ORDER,
UNKNOW
}; static void GBKToUTF16(const std::string& gbkString, std::wstring& utf16String) throw(EncodeException);
static void UTF16ToGBK(const std::wstring& utf16String, std::string& gbkString) throw(EncodeException);
static void UTF8ToUTF16(const std::string& utf8String, std::wstring& utf16String) throw(EncodeException);
static void UTF16ToUTF8(const std::wstring& utf16String, std::string& utf8String) throw(EncodeException);
static void UTF8ToGBK(const std::string& utf8String, std::string& gbkString) throw(EncodeException);
static void GBKToUTF8(const std::string& gbkString, std::string& utf8String) throw(EncodeException);
static void EncodeHexString(const std::string& bytes, std::string& hexString);
static void DecodeHexString(const std::string& hexString, std::string& bytes);
static void EncodeHexString(const std::wstring& bytes, std::string& hexString);
static void DecodeHexString(const std::string& hexString, std::wstring& bytes);
static ByteOrderType GetCurrentByteOrder(); private:
static Poco::UnicodeConverter _unicodeConverter;
static ByteOrderType _currentByteOrder;
};
/********Encoding.cpp********/#include "Encoding.h"
#include "Poco/NumberParser.h" Poco::UnicodeConverter Encoding::_unicodeConverter;
Encoding::ByteOrderType Encoding::_currentByteOrder; POCO_IMPLEMENT_EXCEPTION(EncodeException, Poco::Exception, "Encoding error") void Encoding::GBKToUTF16(const std::string& gbkString, std::wstring& utf16String)
{
//获得需要分配的空间大小
int size = MultiByteToWideChar(, , gbkString.c_str(), -, NULL, );
std::vector<wchar_t> buff(size);
if(MultiByteToWideChar(, , gbkString.c_str(), -, buff.data(), size) == )
{
//throw a exception
throw EncodeException("GBK convert to UTF16 failed", GetLastError());
}
if(!utf16String.empty())
utf16String.clear();
utf16String.append(buff.data(), buff.size());
} void Encoding::UTF16ToGBK(const std::wstring& utf16String, std::string& gbkString)
{
int size = ; //获得需要分配的空间大小
size = WideCharToMultiByte(, , utf16String.c_str(), -, NULL, , NULL, NULL);
std::vector<char> buff(size);
if(WideCharToMultiByte(, , utf16String.c_str(), -, buff.data(), size, NULL, NULL) == )
throw EncodeException("UTF16 convert to GBK failed", GetLastError()); if(!gbkString.empty())
gbkString.clear();
gbkString.append(buff.data(), buff.size());
} void Encoding::UTF8ToUTF16(const std::string& utf8String, std::wstring& utf16String)
{
std::string errorMessage; try
{
_unicodeConverter.toUTF16(utf8String, utf16String);
}
catch(Poco::Exception &e)
{
errorMessage.append("UTF8 convert to UTF16 failed, ");
errorMessage.append(e.message());
}
if(!errorMessage.empty())
throw EncodeException(errorMessage, GetLastError());
} void Encoding::UTF16ToUTF8(const std::wstring& utf16String, std::string& utf8String)
{
std::string errorMessage; try
{
_unicodeConverter.toUTF8(utf16String, utf8String);
}
catch(Poco::Exception &e)
{
errorMessage.append("UTF8 convert to UTF16 failed, ");
errorMessage.append(e.message());
}
if(!errorMessage.empty())
throw EncodeException(errorMessage, GetLastError());
} void Encoding::UTF8ToGBK(const std::string& utf8String, std::string& gbkString)
{
std::wstring utf16String;
std::string errorMessage; try
{
_unicodeConverter.toUTF16(utf8String, utf16String);
UTF16ToGBK(utf16String, gbkString);
}
catch(EncodeException)
{
errorMessage = "UTF8 convert to GBK failed";
}
catch(Poco::Exception &e)
{
errorMessage.append("UTF8 convert to UTF16 failed, ");
errorMessage.append(e.message());
}
if(!errorMessage.empty())
throw EncodeException(errorMessage, GetLastError());
} void Encoding::GBKToUTF8(const std::string& gbkString, std::string& utf8String)
{
std::wstring utf16String;
std::string errorMessage; try
{
GBKToUTF16(gbkString, utf16String);
_unicodeConverter.toUTF8(utf16String, utf8String);
}
catch(EncodeException)
{
errorMessage = "GBK convert to UTF8 failed";
}
catch(Poco::Exception &e)
{
errorMessage.append("UTF8 convert to UTF16 failed, ");
errorMessage.append(e.message());
}
if(!errorMessage.empty())
throw EncodeException(errorMessage, GetLastError());
} void Encoding::EncodeHexString(const std::string& bytes, std::string& hexString)
{
if(!hexString.empty())
hexString.clear(); Poco::DigestEngine::Digest digest(bytes.begin(), bytes.end());
hexString = Poco::DigestEngine::digestToHex(digest);
} void Encoding::DecodeHexString(const std::string& hexString, std::string& bytes)
{
unsigned int _value;
if(!bytes.empty())
bytes.clear(); for(std::string::size_type i = , j = ; i < hexString.length(); i+=)
{
if(NumberParser::tryParseHex(hexString.substr(i, ), _value))
bytes.push_back(_value);
}
} void Encoding::EncodeHexString(const std::wstring& utf16String, std::string& hexString)
{
if(!hexString.empty())
hexString.clear(); Poco::DigestEngine::Digest digest;
for(auto iter = utf16String.begin(); iter != utf16String.end(); ++iter)
{
const unsigned char* ptr = (const unsigned char*)&*iter;
if(GetCurrentByteOrder() == BIG_ENDIAN_BYTE_ORDER)
{
digest.push_back(*ptr);
digest.push_back(*(ptr + ));
}
else if(GetCurrentByteOrder() == LITTLE_ENDIAN_BYTE_ORDER)
{
digest.push_back(*(ptr + ));
digest.push_back(*ptr);
}
else
return;
} hexString = Poco::DigestEngine::digestToHex(digest);
} void Encoding::DecodeHexString(const std::string& hexString, std::wstring& utf16String)
{
unsigned int _value;
if(!utf16String.empty())
utf16String.clear(); for(std::string::size_type i = , j = ; i < hexString.length(); i+=)
{
if(NumberParser::tryParseHex(hexString.substr(i, ), _value))
utf16String.push_back(_value);
}
} Encoding::ByteOrderType Encoding::GetCurrentByteOrder()
{
static bool flag = false;
if(flag)
return _currentByteOrder; union
{
char16_t s;
char c[];
}un; un.s = 0x0102;
if(un.c[] == && un.c[] == )
_currentByteOrder = BIG_ENDIAN_BYTE_ORDER;
else if(un.c[] == && un.c[] == )
_currentByteOrder = LITTLE_ENDIAN_BYTE_ORDER;
else
_currentByteOrder = UNKNOW; flag = true;
return _currentByteOrder;
}

基于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. C#用ado.net访问EXCEL的常见问题及解决方法

    C#用ado.net访问EXCEL的常见问题及解决方法,除了像sql server,access常见的数据库,其实Excel文件也可以做为数据库访问. ado.net访问excel的实例: OleDb ...

  2. Js控制弹窗实现在任意分辨率下居中显示

    弹窗居中比较烦人的是怎么才能在任意分辨率下实现居中显示.1,html部分 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transition ...

  3. modelsim 保存波形文件

    1. do文件记录了仿真的过程和加载的各种库. do文件的保存过程: file——>save format——>D:/modeltech_6.5b/examples/run_wave.do ...

  4. spring mvc 2.5.6配置

    兼容公司老版本号项目.必须得用spring mvc2.5.6,那么问题来了. 怎么配置controller都抛出no mapping的错误.经过查文档得出下面配置.仅供參考. servlet-conf ...

  5. cocos2d-x中几种存储数据的方式

    说明:本文所论述内容均基于cocos2dx 3.0 版本. 1.UserDefault 它是cocos2d-x用来存取基本数据类型用的.保存为XML文件格式. 查看CCUserDefault文件,可以 ...

  6. Http请求中Content-Type讲解

    http://blog.csdn.net/weichuang_1/article/details/50451496 ****************************************** ...

  7. 推荐2个干净的PE

    1,微PE(之前著名的通用PE.绝对PE都出自该作者) 官网:http://www.wepe.com.cn/ 2,金狐维护盘 官网:http://www.jinhu.me/

  8. JS移动li行数据,点击上移下移(是位置的互换,不是top的偏移量改变)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. LeetCode: Letter Combinations of a Phone Number 解题报告

    Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...

  10. RabbitMQ(二):mandatory标志的作用

    本文转自:http://m.blog.csdn.net/article/details?id=54311277 在生产者通过channel的basicPublish方法发布消息时,通常有几个参数需要设 ...