笔试题之三

获得数字字符串的8BIT


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

void cbd(char *arr,int len)
{
int i;
int temp=0;

if(len%2==0)
{
for(i=0;i < len;i++)
{
int j;
for(j=0;j < 4;j++)
{
if(i%2==0 && arr[i]-'0'&(1 << (4-j-1)))
{
temp=temp+(128 >> j);
}
if(i%2!=0&&arr[i]-'0'&(1 << 4-j-1))
{
temp=temp+(8>>j);
}
if(i%2!=0&&j==3)
{
printf("%d",temp);
temp=0;
}

}
}
}
else
{
printf("%d",arr[0]-'0');
for(i=1;i  <  len;i++)
{
int j;
int flag=1;
for(j=0;j  <  4;j++)
{
if(i%2!=0 && arr[i]-'0'&(1  <<  (4-j-1)))
{
temp=temp+(128>>j);
flag=0;
}
if(i%2==0&&arr[i]-'0'&(1  <<  4-j-1))
{
temp=temp+(8 >> j);
}
if(i%2==0&&j==3)
{
printf("%d",temp);
temp=0;
}

}
}
}
}
int main()
{
char arr[100];
gets(arr);
int len =strlen(arr);
cbd(arr,len);
return 0;
}
posted @ 2014-09-09 17:41  dreamsyeah  阅读(127)  评论(0编辑  收藏  举报