用最土的方法将CString 转换成 int
程序员文章站
2022-04-27 08:11:36
...
用最土的方法将CString 转换成 int
1.
“E5”
2.
‘E’ = 69-55 = E (十六进制) = 14(十进制)x16 = 0xE0
3.
‘5’ = 53 - ‘0’(48) = 0x05
4.
0xE0 + 0x05 = 0xE5
unsigned int StrToInt(CString str)
{
unsigned int a,b;
signed short n,m,k;
unsigned char c;
a = b = 0;
k = 0;
for (n = str.GetLength(); n >0 ; n--)
{
c = str.GetAt(k);
if ((c >= '0') && (c <= '9'))
{
b = c - '0';
for (m = 1; m < n; m++)
{
b = b * 16;
}
}
else if ((c >= 'a') && (c <= 'f'))
{
b = c - 87;
for (m = 1; m < n; m++)
{
b = b * 16;
}
}
else if ((c >= 'A') && (c <= 'F'))
{
b = c - 55;
for (m = 1; m < n; m++)
{
b = b * 16;
}
}
k++;
a += b;
}
return a;
}