如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
BOOL IsHexFormat( LPCTSTR pStr)
{
if (pStr[0] == L '0' && ((pStr[1] == L 'x' ) || (pStr[1] == L 'X' ))){
return TRUE;
}
return FALSE;
}
BOOL IsInputValid( LPCTSTR pStr)
{
int i;
BOOL res;
BOOL IsHex;
i = 0;
res = TRUE;
IsHex = IsHexFormat(pStr);
while (pStr[i] != L '\0' ){
if (pStr[i] >= L '0' && pStr[i] <= L '9' ){
i++;
continue ;
}
else if (IsHex && (i == 1)){
i++;
continue ;
}
else if (IsHex &&
((pStr[i] >= L 'a' && pStr[i] <= L 'f' ) ||
(pStr[i] >= L 'A' && pStr[i] <= L 'F' ) )) {
i++;
continue ;
}
else {
res = FALSE;
break ;
}
}
return res;
}
UINT32 CStrHex2Uint32( LPCTSTR pStr)
{
int i = 0;
UINT32 res = 0;
while (pStr[i] != L '\0' ){
if (pStr[i] >= L '0' && pStr[i] <= L '9' ){
res = res * 16 + pStr[i] - L '0' ;
}
else if (pStr[i] >= L 'a' && pStr[i] <= L 'f' ){
res = res * 16 + pStr[i] - L 'a' + 10;
}
else if (pStr[i] >= L 'A' && pStr[i] <= L 'F' ){
res = res * 16 + pStr[i] - L 'A' + 10;
}
else {
break ;
}
i++;
}
return res;
}
/* 将CString转化成UINT32, 0x开头的识别成十六进制,其它为十进制*/
BOOL CStr2Uint32(CString str, UINT32 *pData)
{
LPCTSTR pStr;
pStr = ( LPCTSTR )str;
if (!IsInputValid(pStr)){
*pData = 0;
return FALSE;
}
if (IsHexFormat(pStr)){
UINT32 Data;
pStr = &pStr[2];
*pData = CStrHex2Uint32(pStr);
}
else {
*pData = _wtoi(( wchar_t *)pStr);
}
return TRUE;
}
|
以上就是小编为大家带来的将CString字符串输入转化成整数的实现方法的全部内容了,希望对大家有所帮助,多多支持服务器之家~