#include "Coding.h"
using namespace Text;
#pragma warning ( disable: 4333)
namespace _utf8_
{
void encode(const wchar_t * str,unsigned long strlen ,unsigned char * buffer )
{
while( strlen > 0 )
{
if( *str < 0x80 ) //0xxxxxxx
{
*buffer =(unsigned char)*str;
buffer++;
}
else if( *str<0x0800) //双字节模板 110xxxxx 10xxxxxx
{
*buffer = ( *str >> 6 | 0xc0 );
buffer[1] = ( *str & 0x3f | 0x80 );
buffer+=2;
}
else if( *str < 0x010000 ) //三字节模板 1110xxxx 10xxxxxx 10xxxxxx
{
*buffer = ( *str >> 12 | 0xe0);
buffer[1] = ( *str >> 6 & 0x3f | 0x80);
buffer[2] = ( *str & 0x3f | 0x80);
buffer+=3;
}
else if ( *str < 0x200000 ) //四字节模板 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
{
*buffer = (*str >> 18 | 0xf0);
buffer[1] = (*str >> 12 & 0x3f | 0x80);
buffer[2] = (*str >> 6 & 0x3f | 0x80);
buffer[3] = (*str & 0x3f | 0x80);
buffer+=4;
}
else if ( *str < 0x04000000) //五字节模板 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
{
*buffer = ( *str>>24 | 0xf8 );
buffer[1] = ( *str>>18 & 0x3f | 0x80 );
buffer[2] = ( *str>>12 & 0x3f | 0x80 );
buffer[3] = ( *str>>6 & 0x3f | 0x80 );
buffer[4] = ( *str &0x3f | 0x80 );
buffer+=5;
}
else //六字节模板 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
{
*buffer = ( *str>>30 | 0xfc );
buffer[1] = ( *str>>24 & 0x3f | 0x80 );
buffer[2] = ( *str>>18 & 0x3f | 0x80 );
buffer[3] = ( *str>>12 & 0x3f | 0x80 );
buffer[4] = ( *str>>6 & 0x3f | 0x80 );
buffer[5] = ( *str &0x3f | 0x80 );
buffer+=6;
}
strlen -- ;
str++;
}
}
bool count_force(const unsigned char * bytes ,unsigned long bytes_len,unsigned long &count)
{
if( bytes == 0 )
{
return false;
}
unsigned long tmpcount=0;
while( bytes_len > 0 )
{
if( *bytes < 0x80 ) //0xxxxxxx
{
bytes++;
bytes_len --;
}
else if ( ((*bytes) >> 5) ==6 ) //110xxxxx 10xxxxxx
{
if(bytes_len >1 && bytes[1]>>6 == 2 )
{
bytes+=2;
bytes_len -=2;
}
}
else if ( ((*bytes) >> 4) == 14 ) //1110xxxx 10xxxxxx 10xxxxxx
{
if( bytes_len >2 && bytes[1]>>6==2 && bytes[2]>>6==2 )
{
bytes+=3;
bytes_len -=3;
}
}
else if ( ((*bytes) >> 3) == 30 ) //11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
{
if( bytes_len >3 && bytes[1]>>6==2 && bytes[2]>>6==2 && bytes[3]>>6==2 )
{
bytes+=4;
bytes_len -=4;
}
}
else if ( ((*bytes) >> 2) == 62 ) //111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
{
if( bytes_len >4 && bytes[1]>>6==2 && bytes[2]>>6==2 && bytes[3]>>6==2 && bytes[4]>>6==2 )
{
bytes+=5;
bytes_len -=5;
}
}
else if ( ((*bytes) >> 1) == 126 ) //1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
{
if( bytes_len >5 && bytes[1]>>6==2 && bytes[2]>>6==2 && bytes[3]>>6==2 && bytes[4]>>6==2 && bytes[5]>>6==2 )
{
bytes+=6;
bytes_len -=6;
}
}
else //非正常的字节
{
bytes++;
bytes_len --;
}
tmpcount++;
}
count=tmpcount;
return true;
}
bool decode(const unsigned char * bytes ,unsigned long bytes_len ,wchar_t *buffer,bool decode_force=false)
{
while( bytes_len > 0 )
{
if( *bytes < 0x80 ) //0xxxxxxx
{
*buffer = *bytes;
bytes++;
bytes_len --;
}
else if ( ((*bytes) >> 5) ==6 ) //110xxxxx 10xxxxxx
{
if( bytes_len >1 && bytes[1]>>6 == 2 )
{
*buffer = ((*bytes & 0x1f ) << 6) | (bytes[1] & 0x3f);
bytes+=2;
bytes_len -=2;
}
}
else if ( ((*bytes) >> 4) == 14 ) //1110xxxx 10xxxxxx 10xxxxxx
{
if(bytes_len >2 && bytes[1]>>6==2 && bytes[2]>>6==2 )
{
*buffer = ((*bytes & 0x0f ) << 12 ) |
((bytes[1] & 0x3f) << 6 ) |(bytes[2] & 0x3f);
bytes+=3;
bytes_len -=3;
}
}
else if ( ((*bytes) >> 3) == 30 ) //11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
{
if( bytes_len >3 && bytes[1]>>6==2 && bytes[2]>>6==2 && bytes[3]>>6==2 )
{
*buffer = ((*bytes & 0x07 ) << 18 ) |
((bytes[1] & 0x3f) << 12 ) | ((bytes[2] & 0x3f) << 6 ) |(bytes[3] & 0x3f);
bytes+=4;
bytes_len -=4;
}
}
else if ( ((*bytes) >> 2) == 62 ) //111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
{
if( bytes_len >4 && bytes[1]>>6==2 && bytes[2]>>6==2 && bytes[3]>>6==2 && bytes[4]>>6==2 )
{
*buffer = ((*bytes & 0x03 ) << 24 ) |
((bytes[1] & 0x3f) << 18 ) | ((bytes[2] & 0x3f) << 12 ) | ((bytes[3] & 0x3f) << 6 ) |(bytes[4] & 0x3f);
bytes+=5;
bytes_len -=5;
}
}
else if ( ((*bytes) >> 1) == 126 ) //1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
{
if( bytes_len >5 &&bytes[1]>>6==2 && bytes[2]>>6==2 && bytes[3]>>6==2 && bytes[4]>>6==2 && bytes[5]>>6==2 )
{
*buffer = ((*bytes & 0x03 ) << 30 ) |
((bytes[1] & 0x3f) << 24 ) | ((bytes[2] & 0x3f) << 18 ) | ((bytes[3] & 0x3f) << 12 ) |((bytes[3] & 0x3f) << 6 ) |(bytes[5] & 0x3f);
bytes+=6;
bytes_len -=6;
}
}
else //非正常的字节
{
if( decode_force ) //强制解码,将该字节直接提升为宽字符
{
*buffer = *bytes;
bytes++;
bytes_len --;
}
else
{
return false;
}
}
buffer++;
}
return true;
}
}
//获取编码为多字节后多字节的长度(字节数)
bool Utf8Coding::getCount(const wchar_t * str,unsigned long strlen, unsigned long &count)
{
if( str == 0 )
{
return false;
}
count = 0;
while( strlen > 0)
{
if( *str < 0x80 )
{
count ++;
}
else if( *str<0x0800)
{
count +=2;
}
else if( *str < 0x010000 )
{
count +=3;
}
else if ( *str < 0x200000 )
{
count +=4;
}
else if ( *str < 0x04000000)
{
count +=5;
}
else
{
count +=6;
}
str ++;
strlen -- ;
}
return true;
}
bool Utf8Coding::getCount(const unsigned char * bytes ,unsigned long bytes_len , unsigned long &count)
{
if( bytes == 0)
{
return false;
}
unsigned long tmpCount=0;
while( bytes_len > 0 )
{
if( *bytes < 0x80 ) //0xxxxxxx
{
bytes++;
bytes_len --;
}
else if ( ((*bytes) >> 5) ==6 ) //110xxxxx 10xxxxxx
{
bytes+=2;
bytes_len -=2;
}
else if ( ((*bytes) >> 4) == 14 ) //1110xxxx 10xxxxxx 10xxxxxx
{
bytes+=3;
bytes_len -=3;
}
else if ( ((*bytes) >> 3) == 30 ) //11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
{
bytes+=4;
bytes_len -=4;
}
else if ( ((*bytes) >> 2) == 62 ) //111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
{
bytes+=5;
bytes_len -=5;
}
else if ( ((*bytes) >> 1) == 126 ) //1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
{
bytes+=6;
bytes_len -=6;
}
else //非正常的字节
{
return false;
}
tmpCount++;
}
count=tmpCount;
return true;
}
//编码为多字节
bool Utf8Coding::getBytes(const wchar_t * str,unsigned long strlen,ByteArray & output)
{
unsigned long count;
if ( ! getCount(str,strlen,count) )
{
return false;
}
unsigned char *buffer = new unsigned char[count];
if( buffer == 0 )
{
return false;
}
_utf8_::encode(str,strlen,buffer);
if( output.Size != 0 )
{
delete [] output.Elem;
}
output.Size = count;
output.Elem = buffer;
return true;
}
bool Utf8Coding::getString(const unsigned char * bytes ,unsigned long bytes_len ,WCharArray & output,bool forceDecode)
{
unsigned long count;
if(! forceDecode)
{
if( ! getCount(bytes,bytes_len,count) )
{
return false;
}
}
else
{
if( ! _utf8_::count_force(bytes,bytes_len,count) )
{
return false;
}
}
wchar_t *buffer= new wchar_t[count];
if( buffer ==0)
{
return false;
}
if ( ! _utf8_::decode(bytes

缤纷冷泪
- 粉丝: 48
最新资源
- 基于遗传算法的前后端分离在线测试练习系统——SpringBoot+Vue+MySQL+Redis实现自动组卷
- 新能源光伏并网逆变器电流环解耦控制及其MATLABSimulink仿真建模分析 光伏并网逆变器
- 永磁同步电机三矢量模型预测电流控制的深度解析与仿真研究 - PI控制器 精华版
- 新能源复杂环境下三相不平衡正负序分离锁相环(MATLAB仿真)及应用
- 永磁同步电机双矢量MPC模型预测电流控制技术及仿真研究
- 基于STM32F103和FPGA的高效伺服驱动器:电流环处理提升运行效率 - 数字信号处理 参考
- PSRR仿真教程:使用Cadence psspxf对分频器和环形压控振荡器电路进行PSRR仿真评估与优化
- 电机多目标优化与灵敏度分析:基于SALib和响应面模型的参数选择与优化
- 电力电子领域半桥LLC谐振变换器96V转14.4V高效软开关设计与仿真实现
- 带隙基准技术及其仿真的新手实践指南:涵盖温度特性、PSRR、稳定性和噪声仿真 - 带隙基准
- PFC2D软件中接触力组构图自动生成技术及其应用 - 离散元方法 资料
- 基于OpenCV部署yolov8检测人脸和关键点的完整源码含C++和Python两版本
- FLAC-PFC耦合模拟技术在霍普金森杆冲击试验中对SPHB材料动态响应的研究
- 多目标路径规划中蚁群算法的优化与改进策略研究及其实际应用
- C#实现快速傅里叶变换算法
- EtherCAT总线通信:基于STM32 MCU和AX58100 ESC的从站开发方案与实践
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


