基于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 ...
随机推荐
- ubuntu为python处理图片安装图片数据增强库imgaug
1 依赖Required dependencies: six numpy scipy scikit-image (pip install -U scikit-image) OpenCV (i.e. c ...
- 独立成分分析(Independent component analysis, ICA)
作者:桂. 时间:2017-05-22 12:12:43 链接:http://www.cnblogs.com/xingshansi/p/6884273.html 前言 今天群里冒出这样一个问题:群里谁 ...
- git将远程仓库最新版本拉到本地仓库
一.正规做法有两种.git fetch和git pull. 注意不管用fetch还是pull,做之前都要在本地仓库做一次git commit,确保,本地仓库和工作目录及缓存一致.1.git fetch ...
- [sh]函数+条件表达式
了解了下shell的函数和case语句: 函数格式: function(){ } 例子: function rsyncstart() { if [ "${status1}X" == ...
- [na]acl拒绝访问流量审计
acl审计拒绝的流量及拒绝提示 interface Ethernet0/0 ip address 12.1.1.2 255.255.255.0 ip access-group 10 in half-d ...
- 【Android】将Xamarin For VS升级为4.0.1.145版
分类:C#.Android.VS2015 创建日期:2016-03-18 一.卸载原来安装的Xamarin for VS 4.0.0.1717版 下面是Xamarin for VS发布的版本简介: - ...
- canvas.drawBitmap(bitmap, src, dst, paint)
// GameView.drawImage(canvas, mBitDestTop, miDTX, mBitQQ.getHeight(), mBitDestTop.getWidth(), mBitDe ...
- 李洪强iOS开发之-FMDB的用法
// // ViewController.m // 04 - FMDB的用法 // // Created by 李洪强 on 2017/6/6. // Copyright © 2017年 李洪 ...
- 四、用“”或构造函数创建Java的String区别
在Java中,一个字符串可以通过下面两种方法创建. String x = "abc"; String y = new String("abc"); 用双引号创建 ...
- Ribbon重试机制与Hystrix熔断机制的配置问题1
Ribbon超时与Hystrix超时问题,为了确保Ribbon重试的时候不被熔断,我们就需要让Hystrix的超时时间大于Ribbon的超时时间,否则Hystrix命令超时后,该命令直接熔断,重试机制 ...