广告位招租

(2条消息) 输入一个表示十六进制的字符串,转换为十进制的整数输出。_江海细流的博客-CSDN博客_输入一个十六进制字符串

输入一个表示十六进制的字符串,转换为十进制的整数输出。

/***********************************************************************************
将16进制的字符串转换成10进制
1.如果存在0x 必需将 0x 剔除
2.该程序假定是在32位机器上,故16进制为FFFF,不足4个字符串,修正一下,前面空格填0
3.也可以是用strtol 直接完成字符串和16进制的转换
************************************************************************************/
#include<stdio.h>
#include<string.h>
#include<math.h>
/*计算字符串是否有四个字节*/
int GetLeghtofHEX(char *p)
{
    int count=0;
	char *temp =p;
	while(*temp++!='\0')
		count++;
	return count ;
}
/*如果不足四个字节,前4-count位填0*/
char *modifbit(char *p)
{   
	char tab[5];
	int i=0,j=0;
	char * temp;
	int count=GetLeghtofHEX(p);
    if (count!=4)
	{
		for(i=0;i<4-count;i++)
			tab[i]='0';
		while(i<4)
		{
			tab[i]=*(p+j);
			j++;
			i++;
		}
		tab[i]='\0';
		temp = tab;
		
	}
	else
		temp=p;
    return temp;
}
 /*利用16进制和10进制转换公式进行换算*/
int Hexstoinit(char *p)
{
    int i,j=0;
    char string[5]="0000";
	int Dec=0,temp;
	char *tempoint=p;
	char *temp2;
	if(strstr(p,"0x"))
	{
       tempoint=strstr(p,"0x")+2;
	}
    printf("string=%s\n",tempoint);
	temp2=modifbit(tempoint);
   //printf("count=%s\n",temp2);
	tempoint=temp2;
    for(i=3;i>=0;i--)
	{
		string[j++]=*(tempoint+i);
	}
	string[4]='\0' ;
	
	printf("string1=%s\n",string);
	for(j=0;j<4;j++)
	{
		if(string[j]<='9')
		{
         temp=string[j]-'0';
		}
		else if(string[j]=='a'||string[j]=='A')
			temp=10;
		else if(string[j]=='b'||string[j]=='B')
			temp=11;
		else if(string[j]=='c'||string[j]=='C')
			temp=12;
		else if(string[j]=='d'||string[j]=='D')
			temp=13;
		else if(string[j]=='e'||string[j]=='E')
			temp=14;
		else if(string[j]=='f'||string[j]=='F')
			temp=15;
		
		Dec+=temp*pow(16,j);
	}
	printf("string=%d\n",Dec);
	return Dec;
}
void main()
{
  //printf("the Hextoint is %d\n",strtol("0xff",NULL,16));
	printf("the Hextoint is %d\n",Hexstoinit("fff"));
	
	
}

原文链接:https://www.exueyuan.top/1005.html,转载请注明出处。

0
广告位招租

评论0

请先

广告位招租
易学手机界面分析工具上线!可用于安卓手机界面,模拟器界面分析,加速自动化开发!查看详情
没有账号? 注册  忘记密码?

社交账号快速登录

微信扫一扫关注
如已关注,请回复“登录”二字获取验证码

Warning: error_log(/www/wwwroot/exueyuan.top/wp-content/plugins/spider-analyser/#log/log-2511.txt): failed to open stream: No such file or directory in /www/wwwroot/exueyuan.top/wp-content/plugins/spider-analyser/spider.class.php on line 2900