StrToInt && StrToHex && IntToString && 编辑框格式 .

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

 

1 StrToInt

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

 

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

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

    return num;
}

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

 

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

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

 

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

 

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

 

 

例子:

 

.h File:

public:
          CEdit m_edit_p1;
public:
          CString m_p1;

 

.cpp File:

           DDX_Control(pDX, IDC_EDIT1, m_edit_p1);

 

  BYTE temp1[2];
  BYTE temp_0=0,temp_1=0;
  BYTE hex1[2];
  CString str1,str2,str3,str0;
              

  StrToHex(m_p1,1,hex1);
         
  for(int com1=0;com1<1;com1++)
  {
     temp1[com1]=hex1[com1];
  }

 

     if(temp1[0]>=0 && temp1[0]<=63)
  {
         temp1[0]=temp1[0]+1;
         temp_1=temp1[0]/16;
         temp_0=temp1[0]%16;
         str0=IntToString(temp_0);
         str1=IntToString(temp_1); 
         m_edit_p1.SetWindowText( str1+str0 );
  }

 

4 限制编辑框输入格式的函数:

void FormatInput()
{
 char tmp;        
    int TxtLen = 0;
 
 UpdateData(TRUE);
 TxtLen = m_p3.GetLength();
 
 if (TxtLen == 0)
  {
  return;
 }
 if (TxtLen > 2)       
 {
  m_p3.Delete(TxtLen -1);
 }
 else
 {
  tmp = m_p3.GetAt(TxtLen - 1);
  if ((tmp > 'A' - 1) && (tmp < 'F' + 1)||
   (tmp > 'a' - 1) && (tmp < 'f' + 1)||
   (tmp > '0' - 1) && (tmp < '9' + 1))
  {         
   m_p3.MakeUpper();   
  }
  else
  {
   m_p3.Delete(TxtLen - 1);   
            MessageBox("Please input the right data!""/n" "0 ~ 9 ,A(a) ~ F(f)");
  }
 }
 
 UpdateData(FALSE);
 UpdateData(TRUE);
 ((CEdit *)GetDlgItem(IDC_EDIT6 ))->SetSel(m_p3.GetLength(),
  m_p3.GetLength(),TRUE);
}

你可能感兴趣的:(toString)