在C中将二进制转换为数字字符串为十进制数

时间:2012-08-15 14:03:19

标签: c string binary decimal

我编写了一个程序,要求用户使用字符串输入一个数字,然后程序会将该数字转换为十进制,但是当我编译(使用-lm)并运行a时我遇到了问题.out,我得到一个Segmentation故障(核心转储),不确定在哪里看或如何解决它,还有一个问题我需要什么,以便它打印转换的结果(printf(“东西......”) ))?

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

int main()
{

char string[100];
int s;
char a;
char j;
int sum;

printf("B = B to D\n");
printf("D = D to B\n");
printf("choose which one to convert to:");
scanf("%c%c", &a, &j);

if (a == 'B')
{
    printf("enter binary number to convert to decimal: ");
    scanf("%s", string);

    for(s = strlen-1; s >= 0; s--)
    {

            if(string[s] == '1')
            {
            sum = sum + pow(2,s);

            }
    }

}

return 0;

5 个答案:

答案 0 :(得分:1)

你可能想要strlen(string) - 1,而不是strlen - 1。我最好的猜测是你的程序将strlen解释为一个函数指针,并且在此之后发生了疯狂的事情。

实际上,您可能会对strtol功能感兴趣,这似乎与您正在寻找的功能完全相同。

答案 1 :(得分:0)

您使用strlen作为整数。我认为你的意思是strlen(字符串)

答案 2 :(得分:0)

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <stdlib.h>

void reverse_string(char *string)
{
   int string_length = strlen(string);
   char temp;
   int i;
   for (i = 0; i < string_length/2; i++)
   {
      temp = string[i];
      string[i] = string[string_length - (i + 1)];
      string[string_length - (i + 1)] = temp;
   }
}

int main()
{

   char string[100];
   int s;
   char a;
   char j;
   int sum = 0;
   int string_length = 0;
   int number, original_number;
   int remainder;
   char binary_string[200];
   int i = 0;

   printf("B = B to D\n");
   printf("D = D to B\n");
   printf("choose which one to convert to:");
   scanf("%c%c", &a, &j);
   a = toupper(a);
   if (a == 'B')
   {
      printf("enter binary number to convert to decimal: ");
      scanf("%s", string);

      string_length = strlen(string);
      for(s = strlen(string)-1; s >= 0; s--)
      {
         if(string[s] == '1')
         {
            sum = sum + pow(2,string_length - (s + 1));
         }
      }
      printf("%s in binary is %d\n",string,sum);
   }
   else if (a == 'D')
   {
      printf("enter positive decimal number to convert to binary: ");
      scanf("%s",string);
      number = atoi(string);
      original_number = number;
      if ( number < 0 )
      {
         printf("ERROR: only positive numbers please\n");
         return 1;
      }
      do
      {
         remainder = number % 2;
         if ( remainder == 0 )
            binary_string[i] = '0';
         else
            binary_string[i] = '1';
         number = number / 2;
         i += 1;
      }
      while (number > 0);
      binary_string[i] = '\0';
      reverse_string(binary_string);
      printf("decimal %d is %s in binary\n",original_number,binary_string);
   }
   return 0;
}

答案 3 :(得分:0)

for(sum=0, j=0, s=strlen(string)-1; s >= 0; s--, ++j){
    if(string[s] == '1'){
        sum = sum + pow(2,j);
    }
}
printf("%d\n",sum);

答案 4 :(得分:0)

strlen使用不当。我想你想做像strlen(string)-1这样的事情。 BTW你的逻辑不能转换小数部分。检查此代码:

#include <stdio.h>
#define MAX 1000

int main()
{
    double fraDecimal=0.0,dFractional=0.0 ,fraFactor=0.5;
    long dIntegral = 0,bIntegral=0,bFractional[MAX];
    long intFactor=1,remainder,i=0,k=0,flag=0;
    char fraBinary[MAX];

    printf("Enter any fractional binary number: ");
    scanf("%s",&fraBinary); 
    while(fraBinary[i]) //Separating the integral and fractional parts
    {

      if(fraBinary[i] == '.')
          flag = 1; //If dot is found start taking the fractional part.
      else if(flag==0)
          bIntegral = bIntegral * 10 + (fraBinary[i] -48); 
          /* char - 48 to get the numerical value.*/
      else
          bFractional[k++] = fraBinary[i] -48;
      i++;
    }

    while(bIntegral!=0){
     remainder=bIntegral%10;
     dIntegral= dIntegral+remainder*intFactor;
     intFactor=intFactor*2;
     bIntegral=bIntegral/10;
    }

    for(i=0;i<k;i++){
     dFractional  = dFractional  + bFractional[i] * fraFactor;
     fraFactor = fraFactor / 2;
    }

    fraDecimal = dIntegral + dFractional ;

    printf("Equivalent decimal value: %Lf",fraDecimal);   
    return 0;
}

来源:  C Program to Convert Binary into Decimal Number