188金宝搏官网想用VC6.0做个16进制转换为10进制的计算器。(已解决)

    
因为工作需要经常要用到16进制转换为10进制、8进制和2进制,所以现在想自己写一个进制转换工具程序,现在的问题主要是如何判断EDIT控件内的数是否16进制数。代码如下:

以前写过类似的函数,最近又用到了,简单总结一下,以备后用。

CString a;

 

unsigned
long b=0;

1 StrToInt

unsigned
long c=0;

     此函数将编辑框中输入的字符串,如“1001”,转化为十进制数字,如1001。

unsigned
long q=0;

 

GetDlgItemText(IDC_EDIT2,a); //è¡μñà¼-¿òÄúËùóDÎı¾

int StrToInt(const char* str)
{
 int num = 0;
 BOOL RIGHT = FALSE;
 if(str!=NULL)
 {
    const char* digit = str;
    while((* digit != ‘/n’))
    {
      if(*digit >= ‘0’ && *digit <= ‘9’)
   {
    num = num * 10 +(*digit – ‘0’);
    digit++;
             RIGHT = TRUE;
   }
   else
   {
    break;
   }
    }
  
 }

a.MakeUpper();//è«2¿×a»»Îa′óD′

    if(RIGHT == FALSE)
 {
    num = -1;
 }

for(int
i=0;i<a.GetLength();i++)

    return num;
}

{

2 当初在使用的时候,这两个函数是在MFC框架下,结合编辑框使用的,主要用来将编辑框中输入的数字字符串转换为十六进制数字,从而在程序中进行处理。

if(a.GetAt(i)>=’A’ && a.GetAt(i)<=’F’) b=a.GetAt(i)-55; 

 

else
if(a.GetAt(i)>=’0′ && a.GetAt(i)<=’9′) b=a.GetAt(i)-0x30;

void StrToHex(CString str,int outlen,unsigned char *databuff)
{
 int temp=0;
 for (int i = 0; i < outlen; i++)
 {
  CString StrChar = str.Mid(2 * i,2);     
  sscanf_s(StrChar,”%x”,&temp);          
  databuff[i] = (unsigned char)temp;
 }
}

else

void StrToHex2(CString str,int outlen,unsigned char *databuff)
{
 int temp=0;
 for (int i = 0; i < outlen; i++)
 {
  CString StrChar = str.Mid(2 * i,2);      
  sscanf_s(StrChar,”%d”,&temp);
  databuff[i] = (unsigned char)temp;
 }
}

{

 

MessageBox(“êäèëμØÖ·2»Õyè·£¡”,NULL,MB_OKCANCEL);

3 下面的函数是将十进制数字转换为十六进制数字,或者说是字符的形式,将十进制数字转换后输出到对应的编辑框中显示出来。

exit(1);

 

}

CString IntToString(int dec)
{
      switch (dec)
            {
                case 0:
                    return “0”;
                    break;
                case 1:
                    return “1”;
                    break;
                case 2:
                    return “2”;
                    break;
                case 3:
                    return “3”;
                    break;
                case 4:
                    return “4”;
                    break;
                case 5:
                    return “5”;
                    break;
                case 6:
                    return “6”;
                    break;
                case 7:
                    return “7”;
                case 8:
                    return “8”;
                case 9:
                    return “9”;
                    break;
                case 10:
                    return “A”;
                    break;
                case 11:
                    return “B”;
                    break;
                case 12:
                    return “C”;
                    break;
                case 13:
                    return “D”;
                    break;
                case 14:
                    return “E”;
                    break;
                case 15:
                    return “F”;
                    break;
                default:
                    return “”;
         }
}

q=(unsigned
long)pow(16,a.GetLength()-i-1);

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注