十六进制字符串转十进制整数,诡异的问题,Linux和Windows结果不同

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???!!!

    原文作者:进制转换
    原文地址: https://blog.csdn.net/vanloswang/article/details/39584425
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞