基于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 ...
随机推荐
- django的hello world 项目
一.新建一个django项目bester: django-admin startproject bester 二.在bester项目中建一个叫polls的应用程序: cd bester/ python ...
- unity, 最简单的additive shader
Shader "Custom/myAdditive" { Properties { _MainTex ("Albedo (RGB)&q ...
- cocos2dx 3.3将坐标由父空间转化到局部空间
设在node的父空间内有一点p,要求其转化到node局部空间后的坐标p_local,代码如下: node->getNodeToParentTransform();//in order node- ...
- date 增加一个小时 减少一个小时
# date -s `date -d -1hour +%T`
- [转] 职业规划:一个老鸟眼中“IT民工”的发展方向
IT行业“挣钱太容易”,“IT不像政府管房产这么严,想干嘛就干嘛,另外都跑到境外去上市,没干两年市值翻好多倍,利润比地产高出几十倍几千倍,我们投入10块钱赚1块钱,IT行业投入10块钱赚1000块钱, ...
- 每日英语:First Offer: Take It Or Keep Waiting?
Anyone who has searched for a job fresh out of college knows how difficult it is to get that first j ...
- C/C++数组名与指针区别深入探索(转载)
转载自: C/C++数组名与指针区别深入探索 引言 指针是C/C++语言的特色,而数组名与指针有太多的相似,甚至很多时候,数组名可以作为指针使用.于是乎,很多程序设计者就被搞糊涂了.而许多的大学老师, ...
- Android AlarmManager的一些问题
我开始的代码是这样写的 alarmManager.set(AlarmManager.RTC_WAKEUP, (5*1000), sender); 我的本意是设定五秒后启动闹钟 但是每次都是我设置完闹钟 ...
- xml相关术语说明
<project xmlns="http://maven.apache.org/POM/4.0.0" --命名空间,类似包名,因为xml的标签可自定义,需要命名空间来区分x ...
- Hadoop 新 MapReduce 框架 Yarn 详解【转】
[转自:http://www.ibm.com/developerworks/cn/opensource/os-cn-hadoop-yarn/] 简介: 本文介绍了 Hadoop 自 0.23.0 版本 ...