使用字符串从十进制数转换为二进制数系统

时间:2012-08-16 00:17:04

标签: 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;
    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;
}

3 个答案:

答案 0 :(得分:1)

这里有一些问题。首先,你第一次检查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中,因此您也需要这样做。

答案 1 :(得分:1)

最简单的事情可能是将字符串输入转换为适当的整数(使用例如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);

答案 2 :(得分:1)

如果您希望将负数打印为带有符号的二进制数字字符串,则此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_MAX-INT_MAXINT_MIN(假设32位int):

B = B to D
D = D to B
choose which one to convert to: D
enter decimal number to convert to binary: 2147483647
the binary number is: 1111111111111111111111111111111

B = B to D
D = D to B
choose which one to convert to: D
enter decimal number to convert to binary: -2147483647
the binary number is: -1111111111111111111111111111111

B = B to D
D = D to B
choose which one to convert to: D
enter decimal number to convert to binary: -2147483648
the binary number is: -10000000000000000000000000000000

另一方面,如果您想要与该值对应的位模式,那么Joachim Pileborg的答案就是为您做的。

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