字符串16进制转10进制
发表于:2007-07-01来源:作者:点击数:
标签:
主要代码如下: #include int strHexToInt(char* strSource) { int nTemp=0; CString strTemp; strTemp=strSource; for(char cc=@#G@#,dd=@#g@#; cc { if(strTemp.Find(cc,0) !=-1 || strTemp.Find(dd,0) !=-1) { ::MessageBox(NULL,"请输入正确的16进制字符
主要代码如下:
#include
int strHexToInt(char* strSource) { int nTemp=0;
CString strTemp; strTemp=strSource; for(char clearcase/" target="_blank" >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@#: 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; }
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); } m_edit.SetWindowText(""); m_edit.SetFocus(); } |
程序运行截图:
原文转自:http://www.ltesting.net