📄 chinesecode1.cpp
字号:
// ChineseCode1.cpp: implementation of the CChineseCode class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "练习.h"
#include "ChineseCode1.h"
#include <iostream.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CChineseCode::CChineseCode()
{
}
CChineseCode::~CChineseCode()
{
}
void CChineseCode::UTF_8ToUnicode(wchar_t* pOut,char *pText)
{
char* uchar = (char *)pOut;
uchar[1] = ((pText[0] & 0x0F) << 4) + ((pText[1] >> 2) & 0x0F);
uchar[0] = ((pText[1] & 0x03) << 6) + (pText[2] & 0x3F);
return;
}
void CChineseCode::UnicodeToUTF_8(char* pOut,wchar_t* pText)
{
// 注意 WCHAR高低字的顺序,低字节在前,高字节在后
char* pchar = (char *)pText;
pOut[0] = (0xE0 | ((pchar[1] & 0xF0) >> 4));
pOut[1] = (0x80 | ((pchar[1] & 0x0F) << 2)) + ((pchar[0] & 0xC0) >> 6);
pOut[2] = (0x80 | (pchar[0] & 0x3F));
return;
}
void CChineseCode::UnicodeToGB2312(char* pOut,wchar_t uData)
{
WideCharToMultiByte(CP_ACP,NULL,&uData,1,pOut,sizeof(wchar_t),NULL,NULL);
return;
}
void CChineseCode::Gb2312ToUnicode(wchar_t* pOut,char *gbBuffer)
{
::MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,gbBuffer,2,pOut,1);
return ;
}
void CChineseCode::GB2312ToUTF_8(char* pOut,char *pText, int pLen)
{
char buf[4];
int nLength = pLen* 3;
char* rst = new char[nLength];
memset(buf,0,4);
memset(rst,0,nLength);
int i = 0;
int j = 0;
while(i < pLen)
{
//如果是英文直接复制就可以
if( *(pText + i) >= 0)
{
rst[j++] = pText[i++];
}
else
{
wchar_t pbuffer;
Gb2312ToUnicode(&pbuffer,pText+i);
UnicodeToUTF_8(buf,&pbuffer);
unsigned short int tmp = 0;
tmp = rst[j] = buf[0];
tmp = rst[j+1] = buf[1];
tmp = rst[j+2] = buf[2];
j += 3;
i += 2;
}
}
rst[j] = '\0';
//返回结果
strcpy(pOut, rst);
delete []rst;
return;
}
void CChineseCode::UTF_8ToGB2312(char* pOut, char *pText, int pLen)
{
char * newBuf = new char[pLen];
char Ctemp[4];
memset(Ctemp,0,4);
int i =0;
int j = 0;
while(i < pLen)
{
if(pText[i] > 0)
{
newBuf[j++] = pText[i++];
}
else
{
WCHAR Wtemp;
UTF_8ToUnicode(&Wtemp,pText + i);
UnicodeToGB2312(Ctemp,Wtemp);
newBuf[j] = Ctemp[0];
newBuf[j + 1] = Ctemp[1];
i += 3;
j += 2;
}
}
newBuf[j] = '\0';
//pOut = newBuf;
strcpy(pOut, newBuf);
delete []newBuf;
return;
}
BOOL CChineseCode::UC32Encodegsm(WCHAR* strSrc, CString &strDst,int dwSize)
{
/*
int dwSize = strSrc;
WCHAR wchar[128]; // UNICODE串缓冲区
char* temp=new char[128];
CString cs;
cs=change.RightToDiverse(strSrc);
int i=0;
char c;
while(i<cs.GetLength()){
c=cs.GetAt(i);
wchar[i]=WCHAR(atoi(&c));
i++;
}
*/
char* temp=new char[1024];
strDst.Empty();
::WideCharToMultiByte(CP_OEMCP,NULL,strSrc,-1,temp,dwSize,NULL,FALSE);
dwSize=strlen(temp);
int i=0;
while(i<dwSize){
strDst+=temp[i];
i++;
}
return FALSE;
}
int CChineseCode::gsmDecodePdu(const char *pSrc, char *pDst)
{
int nDstLength; // 目标PDU串长度
unsigned char tmp; // 内部用的临时字节变量
unsigned char buf[256]; // 内部用的缓冲区
// TPDU段协议标识、编码方式、用户信息等
// gsmString2Bytes(pSrc, &tmp, 2); // 用户信息长度(TP-UDL)
// pSrc += 2; // 指针后移 通过串口收发短消息
// UCS2解码
nDstLength=strlen(pSrc);
nDstLength = gsmString2Bytes(pSrc, buf, nDstLength); // 格式转换
nDstLength = gsmDecodeUcs2(buf, pDst, nDstLength); // 转换到TP-DU
// nDstLength = gsmDecode7bit(buf, pDst, nDstLength); // 转换到TP-DU
return nDstLength;
}
// 7-bit解码
// pSrc: 源编码串指针
// pDst: 目标字符串指针
// nSrcLength: 源编码串长度
// 返回: 目标字符串长度
int CChineseCode::gsmDecode7bit(const unsigned char *pSrc, char *pDst, int nSrcLength)
{
int nSrc; // 源字符串的计数值
int nDst; // 目标解码串的计数值
int nByte; // 当前正在处理的组内字节的序号,范围是0-6
unsigned char nLeft; // 上一字节残余的数据
// 计数值初始化
nSrc = 0;
nDst = 0;
// 组内字节序号和残余数据初始化
nByte = 0;
nLeft = 0;
// 将源数据每7个字节分为一组,解压缩成8个字节
// 循环该处理过程,直至源数据被处理完
// 如果分组不到7字节,也能正确处理
while (nSrc < nSrcLength)
{
// 将源字节右边部分与残余数据相加,去掉最高位,得到一个目标解码字节
*pDst = ((*pSrc << nByte) | nLeft) & 0x7f;
// 将该字节剩下的左边部分,作为残余数据保存起来
nLeft = *pSrc >> (7 - nByte);
// 修改目标串的指针和计数值
pDst++;
nDst++;
// 修改字节计数值
nByte++;
// 到了一组的最后一个字节
if (nByte == 7)
{
// 额外得到一个目标解码字节
*pDst = nLeft;
// 修改目标串的指针和计数值
pDst++;
nDst++;
// 组内字节序号和残余数据初始化
nByte = 0;
nLeft = 0;
}
// 修改源串的指针和计数值
pSrc++;
nSrc++;
}
*pDst = 0;
// 返回目标串长度
return nDst;
}
// 可打印字符串转换为字节数据
// 如:"C8329BFD0E01" --> {0xC8, 0x32, 0x9B, 0xFD, 0x0E, 0x01}
// pSrc: 源字符串指针
// pDst: 目标数据指针
// nSrcLength: 源字符串长度
// 返回: 目标数据长度
int CChineseCode::gsmString2Bytes(const char *pSrc, unsigned char *pDst, int nSrcLength)
{
for (int i = 0; i < nSrcLength; i++)
{
// 输出高4位
if((*pSrc>='0')&&(*pSrc<='9'))
{
*pDst=(*pSrc-'0')<<4;
}
else
{
*pDst=(*pSrc-'A'+10)<<4;
}
pSrc++;
// 输出低4位
if ((*pSrc>='0')&&(*pSrc<='9'))
{
*pDst |=*pSrc-'0';
}
else
{
*pDst |= *pSrc-'A'+10;
}
pSrc++;
pDst++;
}
// 返回目标数据长度
return nSrcLength / 2;
}
// 字节数据转换为可打印字符串
// 如:{0xC8, 0x32, 0x9B, 0xFD, 0x0E, 0x01} --> "C8329BFD0E01"
// pSrc: 源数据指针
// pDst: 目标字符串指针
// nSrcLength: 源数据长度
// 返回: 目标字符串长度
int CChineseCode::gsmBytes2String(const unsigned char *pSrc, char *pDst, int nSrcLength)
{
const char tab[]="0123456789ABCDEF"; // 0x0-0xf的字符查找表
for (int i = 0; i < nSrcLength; i++)
{
// 输出高4位
*pDst++ = tab[*pSrc >> 4];
// 输出低4位
*pDst++ = tab[*pSrc & 0x0f];
pSrc++;
}
// 输出字符串加个结束符
*pDst = '\0';
// 返回目标字符串长度
return nSrcLength * 2;
}
// UCS2解码
// pSrc: 源编码串指针
// pDst: 目标字符串指针
// nSrcLength: 源编码串长度
// 返回: 目标字符串长度
int CChineseCode::gsmDecodeUcs2(const unsigned char *pSrc, char *pDst, int nSrcLength)
{
int nDstLength; // UNICODE宽字符数目
WCHAR wchar[128]; // UNICODE串缓冲区
// 高低字节对调,拼成UNICODE
for (int i = 0; i < nSrcLength/2; i++)
{
// 先高位字节
wchar[i] = *pSrc++ << 8;
// 后低位字节
wchar[i] |= *pSrc++;
}
//UNICODE串-->字符串
nDstLength = WideCharToMultiByte(CP_ACP, 0, wchar, nSrcLength/2, pDst, 160, NULL, NULL);
// 返回目标字符串长度
return nDstLength;
}
int CChineseCode::gsmEncodeUCS2(CString strSrc, CString &strDst)
{
int nSrcLength = strSrc.GetLength();
int nDstLength; // UNICODE宽字符数目
WCHAR wchar[128]; // UNICODE串缓冲区
CString strT;
strDst.Empty();
// 字符串-->UNICODE串
nDstLength=::MultiByteToWideChar(CP_ACP, 0, strSrc, nSrcLength, wchar, 128);
// 高低字节对调,输出
for(int i = 0; i < nDstLength; i++)
{
// 先输出高位字节
strT.Format("%02X",(UINT)(unsigned char)(TCHAR(wchar[i] >> 8)));
strDst += strT;
strT.Format("%02X",(UINT)(unsigned char)(TCHAR(wchar[i] & 0xff)));
strDst += strT;
}
// 返回目标编码串长度
return 2 * nDstLength;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -