满足混合字符串(汉字和数字等字符)批量(非一个字符一个字符),转换为16进制;同样支持16进制转换为字符串,C++代码;
在VS2010上用MFC编码测试可运行。可用于串口通信数据编码。
void CzhDlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
//CDialogEx::OnOK();
m_ctrlhex.SetWindowTextA("");
m_ctrlZifu.GetWindowTextA(m_strzifu);
//CString strSerial;
m_strhex=OnHanzihex(m_strzifu);
m_ctrlhex.SetWindowTextA(m_strhex);
}
// 汉字批量转16进制
CString CzhDlg::OnHanzihex(CString strhanzi)
{
CString m_strHex=_T("");
CString str1,str2;
CString strtemp;
str2=strhanzi;
//直到分割到最后一个汉字
int lastword=strlen(str2);//str2 = "测试123" lastword = 7
int j=str2.GetLength();//j = 7
//str2=_T("测试123")
for (int i=0;i<j;i++)
{
char ch;
if (i==(j-1))//如果是最后一个字符,说明不是汉字
{
str1=str2.Left(1);
str2=str2.Right(str2.GetLength()-1);
//转换
hanzihex(str1,&strtemp);
m_strHex+=strtemp;
}
else
{
ch=str2.GetAt(0);
if (IsDBCSLeadByte(ch))//判断是否为汉字
{
str1=str2.Left(2);
str2=str2.Right(str2.GetLength()-2);
//转换
hanzihex(str1,&strtemp);
m_strHex+=strtemp;
i++;
}
else
{
str1=str2.Left(1);
str2=str2.Right(str2.GetLength()-1);
//转换
hanzihex(str1,&strtemp);
m_strHex+=strtemp;
}
}
}
return m_strHex;
}
// //单个——汉字转16进制
void CzhDlg::hanzihex(CString instr, CString* outstr)
{
char h,l,t;
if(instr.GetLength()==0) MessageBox("您的汉字输入有误!");
else if(instr.GetLength()==1)
{
t=instr.GetAt(0);
h=(t&0xf0)>>4;if(h>9) h+=0x57;else h+=0x30;
l=(t&0x0f);if(l>9) l+=0x57;else l+=0x30;
*outstr=h;
*outstr+=l;
}
else
{
t=instr.GetAt(0);
h=(t&0xf0)>>4;if(h>9) h+=0x57;else h+=0x30;
l=(t&0x0f);if(l>9) l+=0x57;else l+=0x30;
*outstr=h;
*outstr+=l;
t=instr.GetAt(1);
h=(t&0xf0)>>4;if(h>9) h+=0x57;else h+=0x30;
l=(t&0x0f);if(l>9) l+=0x57;else l+=0x30;
*outstr+=h;
*outstr+=l;
}
}
void CzhDlg::OnBnClickedCancel()
{
// TODO: 在此添加控件通知处理程序代码
//CDialogEx::OnCancel();
m_ctrlZifu.SetWindowTextA("");
m_ctrlhex.GetWindowTextA(m_strhex);
//CString strSerial;
hexTohanzi(m_strhex,&m_strzifu);
m_ctrlZifu.SetWindowTextA(m_strzifu);
}
// 通信信息16进制批量转为汉字
void CzhDlg::hexTohanzi(CString instr, CString* outstr)
{
//处理大小写
int nums=strlen(instr);
CString tmp="",tmp2="";
for (int n=0;n<nums;n++)
{
char ch=instr.GetAt(n);
if (ch>='A'&&ch<='Z')//若为大写
{
tmp.Format("%c",ch+32);
}
else
{
tmp=ch;
}
tmp2+=tmp;
}
instr=tmp2;
/
CString str0,str1,str2;
CString strtemp1;
CString strtemp;
bool order=true;
str2=instr;
//去空格 30 30 30 30 30 31 30 31 B2 E2 CA D4 31 32 23
str2.Remove(' ');
int j=str2.GetLength();
for (int i=1;i<=j;i++)
{
///
str1=str2.Left(4);
str0=str2.Left(2);
if(str0=="")
{
break;
}
else
{
//转换
hexhanzi(str1,&strtemp);
char ch=strtemp.GetAt(0);
if (IsDBCSLeadByte(ch))//若为汉字
{
str2=str2.Right(str2.GetLength()-4);
*outstr+=strtemp;
}
else
{
str2=str2.Right(str2.GetLength()-2);
//转换
hexhanzi(str0,&strtemp);
*outstr+=strtemp;
}
}
}
}
// 16进制转汉字
void CzhDlg::hexhanzi(CString instr, CString* outstr)
{
char t;
char temp[3];
char n,a,b,c,d;
if((instr.GetLength()==0)||(instr.GetLength()==1)||(instr.GetLength()==3)) ;//MessageBox("您的内码输入有误!");
else if(instr.GetLength()==2)
{
t=instr.GetAt(0);
if(t>='0'&&t<='9') n=t-'0';
else if(t>='a'&&t<='f') n=t-'a'+10;
else n=0;
a=n;
t=instr.GetAt(1);
if(t>='0'&&t<='9') n=t-'0';
else if(t>='a'&&t<='f') n=t-'a'+10;
else n=0;
b=n;
temp[0]=a*16+b;
temp[1]='\0';
//m_strHanzi=temp;
*outstr=temp;
}
else
{
t=instr.GetAt(0);
if(t>='0'&&t<='9') n=t-'0';
else if(t>='a'&&t<='f') n=t-'a'+10;
else n=0;
a=n;
t=instr.GetAt(1);
if(t>='0'&&t<='9') n=t-'0';
else if(t>='a'&&t<='f') n=t-'a'+10;
else n=0;
b=n;
t=instr.GetAt(2);
if(t>='0'&&t<='9') n=t-'0';
else if(t>='a'&&t<='f') n=t-'a'+10;
else n=0;
c=n;
t=instr.GetAt(3);
if(t>='0'&&t<='9') n=t-'0';
else if(t>='a'&&t<='f') n=t-'a'+10;
else n=0;
d=n;
temp[0]=a*16+b;
temp[1]=c*16+d;
temp[2]='\0';
*outstr=temp;
}
}
int CzhDlg::hex_char_value(char c)
{
if(c >= '0' && c <= '9')
return c - '0';
else if(c >= 'a' && c <= 'f')
return (c - 'a' + 10);
else if(c >= 'A' && c <= 'F')
return (c - 'A' + 10);
assert(0);
return 0;
}
int CzhDlg::hex_to_decimal(const char* szHex, int len)
{
int result = 0;
for(int i = 0; i < len; i++)
{
result += (int)pow((float)16, (int)len-i-1) * hex_char_value(szHex[i]);
}
return result;
}