将一个4字节的整数的二进制表示中的001替换为011,输出替换后的整数

#include<stdio.h>
#include<string.h>
#include<math.h>
/***将十进制转换成二进制,采用递归方法***/
char * DectoBin(int n)
{   
	   static char bin[32]="0000000000000000000000000000000";
       static i = 0;	   
	   	int temp ;
		temp= n%2;
		n = n/2;
		if(n==0)
		{
		  if(temp==0)
		  {
			  bin[31-i]='0';
			  
		  }
		      
		  else
		  {
			  bin[31-i]='1';
		       
		  }
			  i++;
		  return bin;
		}
		else
		{  
			if(temp==0)
			{
				bin[31-i]='0';
			
			}
			
			else
			{
				bin[31-i]='1';
			
			}
			  i++;
	       DectoBin(n);
		}
		return bin;
}
/***将001替换成011**/

char * replace001(char *s,char *p)
{
	char *temp=s;
	while(temp!=NULL)
	{
		temp=strstr(temp,"001");
		if(temp)
		{
         
		  strncpy(temp,"011",3);
	       temp++;
		}
		
		
	}

	return s;
}
/**将二进制转换成10进制**/
int BintoDec(char *bin)
{
    int i,j=0;
    char string[33]="0000000000000000000000000000000";
	int Dec=0,temp=bin;
    for(i=31;i>=0;i--)
	{
		string[j++]=*(bin+i);
	}
	 string[33]='\0' ;
	    
     printf("string=%s\n",string);
	 for(j=0;j<32;j++)
	 {
		 temp=string[j]-'0';
	     Dec+=temp*pow(2,j);
	 }
		   printf("string=%d\n",Dec);
	return Dec;
}

void main()
{
   char *temp2;
   char *temp=DectoBin(3);
   printf("The binary is %s\n",temp);
   temp2=replace001(temp,"001");
   printf("The binary is %s\n",temp2);
   printf("the int is %d\n",BintoDec(temp2));
   
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江海细流

如该文章对你有帮助,请支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值