将十六进制的字符串转换成整数

#include "stdafx.h"
#include<stdio.h>
#include<string.h>


/*将大写字母转换成小写字母*/
int tolower(int c)
{
	if (c >= 'A' && c <= 'Z')
	{
		return c + 'a' - 'A';
}
	else
	{
		return c;
	}
}


//将十六进制的字符串转换成整数  
long Fixed_key(char s[])
{
	int i;
	int n = 0;
	if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
	{
		i = 2;
	}
	else
	{
		i = 0;
	}
	for (; (s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z'); ++i)
	{
		if (tolower(s[i]) > '9')
		{
			n = 16 * n + (10 + tolower(s[i]) - 'a');
		}
		else
		{
			n = 16 * n + (tolower(s[i]) - '0');
		}
	}
	return n;
}
int main(void)
{
	char time_16[9] = "5afe1e96"; //从二维码获取的十六进制时间戳
	long ten_time = Fixed_key(time_16);
	printf("ten_time:%d\n", ten_time);//ten_time为十进制时间戳

	getchar();
	return 0;
}


猜你喜欢

转载自blog.csdn.net/weibo1230123/article/details/80369683