I have previously written similar functions and recently used them. I would like to summarize them for future use.
1 strtoint
This function converts the string entered in the edit box, such as "1001", into a decimal number, such as 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 when they were in use, these two functions were used in the MFC framework in combination with the editing box. They are mainly used to convert the numeric string entered in the editing box to a hexadecimal number.Program.
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. The following function converts a decimal number to a hexadecimal number, or a character. After the decimal number is converted, it is displayed in the corresponding editing box.
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 "";
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 "";
}
}
Example:
. 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. Restrict functions in the input format in the edit box:
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 ()~ F (f )");
}
}
Updatedata (false );
Updatedata (true );
(Cedit *) getdlgitem (idc_edit6)-> setsel (m_p3.getlength (),
M_p3.getlength (), true );
}