天天看點

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)

  CString StrChar = str.Mid(2 * i,2);      

  sscanf_s(StrChar,"%d",&temp);

3 下面的函數是将十進制數字轉換為十六進制數字,或者說是字元的形式,将十進制數字轉換後輸出到對應的編輯框中顯示出來。

CString IntToString(int dec)

      switch (dec)

            {

                case 0:

                    return "0";

                    break;

                case 1:

                    return "1";

                case 2:

                    return "2";

                case 3:

                    return "3";

                case 4:

                    return "4";

                case 5:

                    return "5";

                case 6:

                    return "6";

                case 7:

                    return "7";

                case 8:

                    return "8";

                case 9:

                    return "9";

                case 10:

                    return "A";

                case 11:

                    return "B";

                case 12:

                    return "C";

                case 13:

                    return "D";

                case 14:

                    return "E";

                case 15:

                    return "F";

                default:

                    return "";

         }

例子:

.h File:

public:

          CEdit m_edit_p1;

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

 ((CEdit *)GetDlgItem(IDC_EDIT6 ))->SetSel(m_p3.GetLength(),

  m_p3.GetLength(),TRUE);