Chinaunix首页 | 论坛 | 博客
  • 博客访问: 628061
  • 博文数量: 603
  • 博客积分: 4000
  • 博客等级: 上校
  • 技术积分: 4940
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-17 11:04
文章分类

全部博文(603)

文章存档

2011年(1)

2008年(602)

我的朋友

分类:

2008-09-17 11:04:56

#include

int strHexToInt(char* strSource)

 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;
  }
 }


 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();
}


--------------------next---------------------

阅读(278) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~