65,211
社区成员
发帖
与我相关
我的任务
分享
Converts a string representing a decimal or hexadecimal number to an integer.
Syntax
BOOL StrToIntEx( LPCTSTR pszString,
STIF_FLAGS dwFlags,
int *piRet
);
Parameters
pszString
[in] A pointer to the null-terminated string to be converted. For further details concerning the
valid forms of the string, see the Remarks section.
dwFlags
[in] One of the following values specifying how pszString should be parsed for its conversion to an
integer.
STIF_DEFAULT
The string at pszString contains the representation of a decimal value.
STIF_SUPPORT_HEX
The string at pszString contains the representation of either a decimal or hexadecimal value. Note
that in hexadecimal representations, the characters A-F are case-insensitive.
piRet
[out] A pointer to a variable of type int that receives the integer value of the converted string.
For instance, in the case of the string "123", the integer pointed to by this value receives the
integer value 123.
If this function returns FALSE, this value is undefined.
If the value returned is too large to be contained in a variable of type int, this parameter
contains the 32 low-order bits of the value. Any high-order bits beyond that are lost.
Return Value
Returns TRUE if the string is converted, or FALSE otherwise.
Remarks
The string pointed to by the pszString parameter must have one of the following forms to be parsed
successfully.
This form is accepted as a decimal value under either flag.
(optional white space)(optional sign)(one or more decimal digits)
These forms are required for hexadecimal values when the STIF_SUPPORT_HEX flag is passed.
(optional white space)(optional sign)0x(one or more hexadecimal digits)
(optional white space)(optional sign)0X(one or more hexadecimal digits)
The optional sign can be the character '-' or '+'; if omitted, the sign is assumed to be positive.
Note If the value is parsed as hexadecimal, the optional sign is ignored, even if it is a '-'
character. For example, the string "-0x1" is parsed as 1 instead of -1.
If the string pointed to by pszString contains an invalid character, that character is considered
the end of the string to be converted and the remainder is ignored. For instance, given the invalid
hexadecimal string "0x00am123", StrToIntEx only recognizes "0x00a", converts it to the integer
value 10, and returns TRUE.
*------------------------------------------------------------
函数名 : TransData(CString InStr, PUCHAR OutStr)
功 能 : 将字符串转换成十六进制数据
参 数 : CString InStr [in] 输入字符串
PUCHAR OutStr [out] 输出转换后数据的指针
返回值 : BOOL,正确返回TRUE,错误返回FALSE
------------------------------------------------------------*/
BOOL TransData(CString InStr, PUCHAR OutStr)
{
UINT i = 0, j = 0;
UINT len = 0;
UCHAR Str[KEY_LEN * 2];
len = InStr.GetLength();
if ((len % 2) != 0)
{
MessageBox("请按要求输入!", "错误", MB_OK | MB_ICONERROR);
return FALSE;
}
memset(OutStr, 0, sizeof(UCHAR[KEY_LEN]));
memset(Str, 0, sizeof(UCHAR[KEY_LEN * 2]));
strcpy((char*)(Str), InStr.GetBuffer(0));
InStr.ReleaseBuffer();
for(i = 0; i < len; i++)
{
if ((Str[i] > 47) && (Str[i] < 58))
{
Str[i] -= 48;
}
else if ((Str[i] > 64) && (Str[i] < 71))
{
Str[i] -= 55;
}
else if ((Str[i] > 96) && (Str[i] < 103))
{
Str[i] -= 87;
}
else
{
MessageBox("数据格式错误!", "错误", MB_OK | MB_ICONERROR);
return FALSE;
}
}
i = 0;
while(i < len)
{ // 转换
Str[j] = Str[i] * 16 + Str[i + 1];
i += 2;
j++;
}