基于Poco的UTF8、UTF16、GBK、Hex之间的转换
/******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之间的转换的更多相关文章
- UTF-8和GBK编码之间的区别(页面编码、数据库编码区别)以及在实际项目中的应用
第一节:UTF-8和GBK编码概述 UTF-8 (8-bit Unicode Transformation Format) 是一种针对Unicode的可变长度字符编码,又称万国码,它包含全世界所有国家 ...
- ASC与HEX之间的转换
ASC与HEX之间的转换 有这么两个函数: 函数 原型 功能 返回值 参数 备注 hex2asc __int16 hex2asc(unsigned char *strhex,unsigned char ...
- C#中的Byte,String,Int,Hex之间的转换函数。
/// <summary> Convert a string of hex digits (ex: E4 CA B2) to a byte array. </summary> ...
- ascii、unicode、utf-8、gbk区别及转换
一.编码 ascii: A:00000010 8位 一个字节 unicode: A:00000000 00000001 00000010 00000100 32位 四个字节 中:00000000 00 ...
- ascii、unicode、utf-8、gbk 区别?
发展史: https://www.cnblogs.com/houxt/p/11250878.html python2内容进行编码(默认ascii),而python3对内容进行编码的默认为utf-8. ...
- UTF8,UTF16,UTF32,UTF16-LE,UTF16-BE,GBK 之间的转换
Unicode是Unicode.org制定的编码标准,目前得到了绝大部分操作系统和编程语言的支持.Unicode.org官方对Unicode的定义是:Unicode provides a unique ...
- 字符编码之间的相互转换 UTF8与GBK(转载)
转载自http://www.cnblogs.com/azraelly/archive/2012/06/21/2558360.html UTF8与GBK字符编码之间的相互转换 C++ UTF8编码转换 ...
- 【miscellaneous】【C/C++语言】UTF8与GBK字符编码之间的相互转换
UTF8与GBK字符编码之间的相互转换 C++ UTF8编码转换 CChineseCode 一 预备知识 1,字符:字符是抽象的最小文本单位.它没有固定的形状(可能是一个字形),而且没有值." ...
- UNICODE与UTF8和GBK之间的关系
http://wenku.baidu.com/link?url=bheGEzfSjEx-QX-ciME5oKooKYE08_NJZ02l2kKFa7kVZJ4t8Ks2uSNByovgP2QL6btq ...
随机推荐
- Logstash怎么导入csv
Logstash.Conf input { file { type => "SSRCode" path => "E:/FTPRootWorkSpace/SD/ ...
- 测试使用Word发布博客
Word发布地址:http://www.cnblogs.com/xwgli/services/metablogapi.aspx
- 【转】sql server存储过程中SELECT 与 SET 对变量赋值的区别
转自:http://www.cnblogs.com/micheng11/archive/2008/07/08/1237905.html SQL Server 中对已经定义的变量赋值的方式用两种,分别是 ...
- [sh]ls -F一种非常有用的ls格式
ls -F一种非常有用的ls格式 tz/y/yupeng > ls -F#q# News/ doc/ images/ mbox ...
- 教你如何写thinkphp多表查询语句
1.table()函数 thinkphp中提供了一个table()函数,具体用法参考以下语句: $list=$Demo->table('think_blog blog,think_type ty ...
- MYSQL COST optimizer
http://blog.chinaunix.net/uid-26896862-id-3326400.html https://www.slideshare.net/olavsa/mysql-optim ...
- solr中时区处理
solr.in.sh中的最后 # By default the start script uses UTC; override the timezone if needed SOLR_TIMEZONE ...
- vue2.0的常用功能简介
路由跳转 当我们想要实现点击链接跳转时,可以使用$router来进行跳转 语法如下: '}}) 这里path是要跳转的路径,query里面是路径跳转时要携带的参数,以对象的形式存在 2 获取路由参数 ...
- 新浪微博5s钟后跳转页面
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- LeetCode: Text Justification 解题报告
Text Justification Given an array of words and a length L, format the text such that each line has e ...