C 使用字符串将十进制转换为二进制

C 使用字符串将十进制转换为二进制,c,string,binary,decimal,C,String,Binary,Decimal,我需要帮助来修复我程序的第二部分,将十进制转换为二进制,这是我到目前为止所拥有的,当我编译它时,我一直得到0,所以我不确定我做错了什么。需要帮忙吗 #include <stdio.h> #include <string.h> #include <math.h> int main() { char string[100]; int s; char a; char j; int sum = 0; int r

我需要帮助来修复我程序的第二部分,将十进制转换为二进制,这是我到目前为止所拥有的,当我编译它时,我一直得到0,所以我不确定我做错了什么。需要帮忙吗

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

int main()
{

    char string[100];
    int s;
    char a;   
    char j;
    int sum = 0;
    int r;
    int q;

    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(string)-1; s >= 0; s--)
        {

            if(string[s] == '1')
            {
                sum = sum + pow(2, strlen(string) - (s +1));
            }
        }
        printf("the decimal number is: %d\n", sum);
    }

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

        while (r > 0)
        {
            r = q%2;
            q = q%2;
        } 

        printf("the binary number is: %d\n", r);

    }

    return 0;
}
#包括
#包括
#包括
int main()
{
字符串[100];
int-s;
字符a;
查尔j;
整数和=0;
INTR;
int-q;
printf(“B=B到D\n”);
printf(“D=D到B\n”);
printf(“选择要转换为哪一个:”);
scanf(“%c%c”、&a和&j);
如果(a=='B')
{
printf(“输入要转换为十进制的二进制数:”);
scanf(“%s”,字符串);
对于(s=strlen(字符串)-1;s>=0;s--)
{
如果(字符串[s]=“1”)
{
sum=sum+pow(2,strlen(字符串)-(s+1));
}
}
printf(“十进制数为:%d\n”,总和);
}
如果(a='D')
{
printf(“输入要转换为二进制的十进制数:”);
scanf(“%s”,字符串);
而(r>0)
{
r=q%2;
q=q%2;
} 
printf(“二进制数为:%d\n”,r);
}
返回0;
}

这里有一些问题。首先,第一次检查r时,它是未初始化的。另一个问题是,每次通过while循环时,都将r和q设置为相同的值。您可能希望q=q/2而不是q=q%2。最后,每次通过循环时都覆盖r,而不是建立一串位。下面是一些您想要执行的伪代码:

 output_string = ""

 while input > 0:
     output_string = concat(input%2, output_string)
     input /= 2

 print output_string

请注意,您也永远不会将读入的字符串转换为整数并将其放入q,因此您也需要这样做。

这里有一些问题。首先,第一次检查r时,它是未初始化的。另一个问题是,每次通过while循环时,都将r和q设置为相同的值。您可能希望q=q/2而不是q=q%2。最后,每次通过循环时都覆盖r,而不是建立一串位。下面是一些您想要执行的伪代码:

 output_string = ""

 while input > 0:
     output_string = concat(input%2, output_string)
     input /= 2

 print output_string

请注意,您也永远不会将读入的字符串转换为整数并将其放入q,因此您也需要这样做。

最简单的事情可能是将字符串输入转换为正确的整数(使用例如
strtol
),然后将该数字转换为仅包含1和0的字符串

比如:

/* Convert a (possibly signed) decimal number in a string to a long integer */
unsigned long number = (unsigned long) strtol(string, NULL, 10);

char output_string[65];  /* If longs are 64 bits, plus one for terminator */
char *output_ptr = output_string;

/* Start with the highest bit, go down to the lowest */
/* sizeof(long) is either 4 or 8 depending on 32 or 64 bit platforms */
/* Multiply with 8 to get the number of bits */
/* -1 because bits are numbered from 0 to 31 (or 63) */
for (int bit = (sizeof(unsigned long) * 8) - 1; bit >= 0; bit--)
{
    /* Using right shift to get the current bit into the lowest position */
    /* Doing bitwise AND to see if the lowest bit is a one or a zero */
    /* Adding '0' makes a a printable ASCII value of a digit */
    *output_ptr++ = ((number >> bit) & 1) + '0';

    /* `*output_ptr` gets the value that `output_ptr` points to */
    /* Then use the `++` operator to increase the pointer */
    /* Now `output_ptr` points to the next character in `output_string` */
}

/* Terminate string */
*output_ptr = '\0';

printf("%ld in binary is %s\n", number, output_string);

最简单的方法可能是将字符串输入转换为一个正确的整数(使用例如
strtol
),然后将该数字转换为只包含1和0的字符串

比如:

/* Convert a (possibly signed) decimal number in a string to a long integer */
unsigned long number = (unsigned long) strtol(string, NULL, 10);

char output_string[65];  /* If longs are 64 bits, plus one for terminator */
char *output_ptr = output_string;

/* Start with the highest bit, go down to the lowest */
/* sizeof(long) is either 4 or 8 depending on 32 or 64 bit platforms */
/* Multiply with 8 to get the number of bits */
/* -1 because bits are numbered from 0 to 31 (or 63) */
for (int bit = (sizeof(unsigned long) * 8) - 1; bit >= 0; bit--)
{
    /* Using right shift to get the current bit into the lowest position */
    /* Doing bitwise AND to see if the lowest bit is a one or a zero */
    /* Adding '0' makes a a printable ASCII value of a digit */
    *output_ptr++ = ((number >> bit) & 1) + '0';

    /* `*output_ptr` gets the value that `output_ptr` points to */
    /* Then use the `++` operator to increase the pointer */
    /* Now `output_ptr` points to the next character in `output_string` */
}

/* Terminate string */
*output_ptr = '\0';

printf("%ld in binary is %s\n", number, output_string);

如果您希望将负数打印为带符号的二进制数字字符串,则此C99代码将完成此操作:

if (a == 'D')
{
    int r;
    printf("enter decimal number to convert to binary: ");
    scanf("%d", &r);
    int i = 0;
    int p = (r >= 0) ? (r = -r, 1) : 0;
    string[i++] = '\0';

    do
    {
        string[i++] = (r % 2) == 0 ? '0' : '1';
        r /= 2;
    } while (r != 0);
    if (!p)
        string[i++] = '-';

    int k = 0;
    while (--i > k)
    {
        char t = string[i];
        string[i] = string[k];
        string[k++] = t;
    }

    printf("the binary number is: %s\n", string);
}
例如,给定
-1234
(十进制),输出为
-10011010010
(二进制)。它还处理两种极端情况:
INT\u MAX
-INT\u MAX
INT\u MIN
(假设为32位
INT
):

另一方面,如果您想要对应于该值的位模式,那么的答案会为您这样做


(这是C99代码,因为它在方便的点声明变量,而不是在C89要求的块的开头。)

如果您希望将负数打印为带符号的二进制数字字符串,则此C99代码将起作用:

if (a == 'D')
{
    int r;
    printf("enter decimal number to convert to binary: ");
    scanf("%d", &r);
    int i = 0;
    int p = (r >= 0) ? (r = -r, 1) : 0;
    string[i++] = '\0';

    do
    {
        string[i++] = (r % 2) == 0 ? '0' : '1';
        r /= 2;
    } while (r != 0);
    if (!p)
        string[i++] = '-';

    int k = 0;
    while (--i > k)
    {
        char t = string[i];
        string[i] = string[k];
        string[k++] = t;
    }

    printf("the binary number is: %s\n", string);
}
例如,给定
-1234
(十进制),输出为
-10011010010
(二进制)。它还处理两种极端情况:
INT\u MAX
-INT\u MAX
INT\u MIN
(假设为32位
INT
):

另一方面,如果您想要对应于该值的位模式,那么的答案会为您这样做


(这是C99代码,因为它在一个块的一部分,而不是在一个块的开始处声明变量,而不是C89所要求的。)

在给它赋值之前,使用
r
,但是我不知道如何首先将字符串除以2…@23ewt3tqa除非在循环中指定它,否则它不是余数。但此时,
while
已经测试了该值。这就是穆萨试图指出的错误。你在给它赋值之前使用
r
。它是余数,但我不知道如何首先让字符串除以2…@23ewt3tqa它不是余数,直到你在循环内部赋值。但此时,
while
已经测试了该值。这就是穆萨试图指出的错误。