十六進制字符串轉十進制整數,詭異的問題,Linux和Windows結果不同
來源:程序員人生 發布時間:2014-10-19 08:00:01 閱讀次數:1837次
static int HexAToInt(char *str, int length)
{
char revstr[16] = { 0 }; //根據十六進制字符串的長度,這里注意數組不要越界
int num[16] = { 0 };
int count = 1;
int result = -1;
if (NULL != str)
{
strncpy(revstr, str, length);
for (int i = length - 1; i >= 0; i--)
{
if ((revstr[i] >= '0') && (revstr[i] <= '9'))
num[i] = revstr[i] - 48;//字符0的ASCII值為48
else if ((revstr[i] >= 'a') && (revstr[i] <= 'f'))
num[i] = revstr[i] - 'a' + 10;
else if ((revstr[i] >= 'A') && (revstr[i] <= 'F'))
num[i] = revstr[i] - 'A' + 10;
else
num[i] = 0;
result = result + num[i] * count;
count = count * 16;//十六進制(如果是八進制就在這里乘以8)
}
}
#ifdef __linux__
result++;
#endif
return result;
}
Why???!!!
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈