#include
int strHexToInt(char* strSource)
{
int nTemp=0;
{
int nTemp=0;
CString strTemp;
strTemp=strSource;
for(char cc='G',dd='g'; cc<='Z',dd<='z'; cc++,dd++) //判断输入的字符串是否合法
{
if(strTemp.Find(cc,0) !=-1 || strTemp.Find(dd,0) !=-1)
{
::MessageBox(NULL,"请输入正确的16进制字符串!","输入错误",MB_ICONEXCLAMATION);
return -1;
}
}
strTemp=strSource;
for(char cc='G',dd='g'; cc<='Z',dd<='z'; cc++,dd++) //判断输入的字符串是否合法
{
if(strTemp.Find(cc,0) !=-1 || strTemp.Find(dd,0) !=-1)
{
::MessageBox(NULL,"请输入正确的16进制字符串!","输入错误",MB_ICONEXCLAMATION);
return -1;
}
}
for(int i = 0; i<(int)::strlen(strSource); i++)
{
int nDecNum;
switch(strSource[i])
{
case 'a':
case 'A': nDecNum = 10; break;
case 'b':
case 'B': nDecNum = 11; break;
case 'c':
case 'C': nDecNum = 12; break;
case 'd':
case 'D': nDecNum = 13; break;
case 'e':
case 'E': nDecNum = 14; break;
case 'f':
case 'F': nDecNum = 15; break;
case '0':
16进制字符串转16进制数组 case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': nDecNum = strSource[i] - '0'; break;
default: return 0;
}
case 'f':
case 'F': nDecNum = 15; break;
case '0':
16进制字符串转16进制数组 case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': nDecNum = strSource[i] - '0'; break;
default: return 0;
}
nTemp += nDecNum * (int)::pow(16,::strlen(strSource)-i -1);
}
return nTemp;
}
}
return nTemp;
}
void CDDlg::OnConvert()
{
UpdateData();
if(strHexToInt((LPSTR)(LPCTSTR)m_str) !=-1)
{
CString str;
str.Format("%d",strHexToInt((LPSTR)(LPCTSTR)m_str));
SetDlgItemText(IDC_STATE,str);
}
{
CString str;
str.Format("%d",strHexToInt((LPSTR)(LPCTSTR)m_str));
SetDlgItemText(IDC_STATE,str);
}
m_edit.SetWindowText("");
m_edit.SetFocus();
}
m_edit.SetFocus();
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论