-
-
[原创]无聊看到的顺手写了个字符串转数字的代码
-
发表于:
2011-11-17 18:39
5847
-
int _hex2int(char* lpStr)
{
unsigned int ret = 0;
do {
unsigned char c = *lpStr;
ret = ret << 4;
if (c >= 'a' && c <= 'f'){
ret += 10 + c - 'a';
} else if (c >= 'A' && c <= 'F'){
ret += 10 + c - 'A';
} else if (c >= '0' && c <= '9'){
ret += c - '0';
} else {
printf("error _hex2int\n");
break;
}
} while(*++lpStr);
printf("\n%x %d\n", ret, ret);
return ret;
}
void _int2str(unsigned int i)
{
char fmt[16] = {0};
int bit_pos = 0;
do {
fmt[14 - bit_pos] = '0' + (i % 10);
bit_pos++;
i = i/10;
} while(i);
printf("%s\n", fmt + 15 - bit_pos);
}
int main(int argc, char** argv)
{
char test[] = "1a12fa";
_int2str(_hex2int(test));
}
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!