繁体   English   中英

将十进制数字的每个数字转换为相应的二进制

[英]convert each digit of a decimal number to correcsponding binary

我需要将字符串“ 12345678”转换为值00010010001101101000101011001111000(二进制值,仅左侧不带零)。 所以我已经用c编写了这段代码,问题是当我运行它时,它什么也没做,只是等待,直到出现错误为止,直到我手动停止它为止。 有任何想法吗?

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

void reduce(char string[]) {
    int i=0, j=0, k=0, cnt=0, tmp=4, num;
    char arr[4], result[4*strlen(string)];
    for (i=0; i<strlen(string); i++) {
            num = atoi(string[i]);
            while (num != 0) {
                    arr[j++] = num%2;
                    num = num/2;
                    tmp--;
            }
            while (tmp != 0) {
                    arr[j++] = 0;
                    tmp--;
            }
            j--;
            for (k=i*4; k<(i*4+4); k++) {
                    result[k++] = arr[j--];
            }
            j = 0;
            tmp = 4;
    }
    printf("The result is: \n");
    for (i=0; i<4*strlen(result); i++) {
            printf("%d",result[i]);
    }
    printf("\n");
}

 int main() {
    char c[8] = "12345678";
    reduce(c);
    return 0;
}

您的代码中有许多小错误,这使得很难查明单个错误。 主要问题似乎是你混淆二进制数字( 01 )用ASCII数字( "0""1" )和被误用字符串函数。

  1. 正如其他地方提到的, char c[8] = ..是错误的。
  2. atoi(string[i])无法工作; 它需要一个字符串 ,而不是char 使用`num = string [i]-'0';
  3. arr[..]获得值'num%2 ,即一个数值。 最好使用'0'+num%2因为它是一个字符串。
  4. 你增量kresult[k++] 已经增加一个循环内k
  5. result[k] = 0; 在打印之前结束,因此strlen可以正常工作
  6. 4*strlen(result)太多了strlen是它的意思。
  7. 您也可以做一个简单的printf("%s\\n", result);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void reduce(char string[]) {
    int i=0, j=0, k=0, cnt=0, tmp=4, num;
    char arr[5], result[4*strlen(string)+1];
    for (i=0; i<strlen(string); i++) {
            num = string[i]-'0';
            while (num != 0) {
                    arr[j++] = '0'+num%2;
                    num = num/2;
                    tmp--;
            }
            while (tmp != 0) {
                    arr[j++] = '0';
                    tmp--;
            }
            arr[j] = 0;
            j--;
            for (k=i*4; k<(i*4+4); k++) {
                    result[k] = arr[j--];
            }
            j = 0;
            tmp = 4;
    }
    result[k] = 0;
    printf("The result is: \n");
    for (i=0; i<strlen(result); i++) {
            printf("%c",result[i]);
    }
    printf("\n");
}

 int main() {
    char c[] = "12345678";
    reduce(c);
    return 0;
}

.. 导致

The result is: 
00010010001101000101011001111000

在您的main() ,执行以下任一操作

char c[ ] = "12345678";

要么

char c[9] = "12345678";

如果要使用c作为字符串。 否则,它没有足够的空间来存储终止空字符。

在这里,我可以自由地修改代码以适合您。 检查以下代码。 希望这是自我解释。

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

void reduce(char string[]) {
        int i=0, j=0, k=0, cnt=0,  count = 0;           //count added, tmp removed
        char arr[4], result[ (4*strlen(string) )+ 1], c;    //1 more byte space to hold null
        for (i=0; i<strlen(string); i++) {
                c = string[i];
                count = 4;
                while (count != 0) {                        //constant iteration 4 times baed on 9 = 1001
                        arr[j++] = '0' + (c%2);            //to store ASCII 0 or 1 [48/ 49]
                        c = c/2;
                        count--;
                }
/*      //not required
                while (tmp >= 0) {
                        arr[j++] = 0;
                        tmp--;
                }
*/
                j--;
                for (k=(i*4); k<((i*4) +4); k++) {
                        result[k] = arr[j--];
                }
                j = 0;
                memset (arr, 0, sizeof(arr));
        }
        result[k] = 0;
        printf("The result is: %s\n", result);   //why to loop when we've added the terminating null? print directly.
/*
        for (i=0; i< strlen(result); i++) {
                printf("%c",result[i]);
        }
        printf("\n");
*/
}

int main() {
        char c[ ] = "12345678";
        reduce(c);
        return 0;
}

输出:

[sourav@broadsword temp]$ ./a.out 
The result is: 00010010001101000101011001111000

从您的示例看来,您尝试的转换是二进制编码的十进制而不是二进制 在这种情况下,您的解决方案有些复杂。 您只需要将每个数字转换为其整数值,然后将位模式转换为ASCII 10

#include <stdio.h>

void reduce( const char* c )
{
    for( int d = 0; c[d] != 0; d++ )
    {
        int ci = c[d] - '0' ;

        for( unsigned mask = 0x8; mask != 0; mask >>= 1 )
        {
            putchar( (ci & mask) == 0 ? '0' : '1' )   ;
        }
    }
}

另一方面,如果您确实打算转换为二进制(而不是BCD),那么如果将整个字符串转换为整数 ,则可以将位模式直接转换为ASCII 10 ,如下所示:

#include <limits.h>
#include <stdlib.h>
#include <stdio.h>


void reduce( const char* c )
{
    unsigned ci = (unsigned)atoi( c ) ;
    static const int BITS = sizeof(ci) * CHAR_BIT ;

    for( unsigned mask = 0x01 << (BITS - 1); mask != 0; mask >>= 1 )
    {
        putchar( (ci & mask) == 0 ? '0' : '1' )   ;
    }
}
  1. 使用int num = atoi(c)将字符串转换为整数。
  2. 然后做

     int binary[50]; int q = num,i=0; while(q != 0) { binary[i++] = q%2; q = q/2; } 

以相反的顺序打印二进制数组将具有等效的二进制文件。 完整程序:

#include<stdio.h>


    int main(){

        char c[100];
        int num,q;

        int binary[100],i=0,j;


        scanf("%d",c);

        num = atoi(c);
        q = num;


        while(q!=0){

             binary[i++]= q % 2;

             q = q / 2;

        }




        for(j = i -1 ;j>= 0;j--)

             printf("%d",binary[j]);


        return 0;

    }

您可以使用以下减少功能。

void reduce(char string[])
{
    unsigned int in = atoi(string) ;
    int i = 0, result[32],k,j;

    while (in > 0) {
        j = in % 10; 
        k = 0;
        while (j > 0) {
            result[i++] = j % 2;
            j = j >> 1;
            k++;
        }
        while (k < 4) {
            result[i++] = 0;
            k++;
        }
        in = in/10; 
    }
    printf("Result\n");
    for(--i;i >= 0; i--) {
        printf("%d", result[i]);
    }
    printf("\n");

}

对于12345678 ,输出将为000100100011010001000101011001111000 ,其中每个字符均以其二进制格式打印。

它可能需要进行一些调整,但它确实可以正常工作。

#include <stdio.h>
#include <stdlib.h>

int
main(void)
{
        int  i;
        int  n;
        char *str       = "12345678";
        const int  bit  = 1 << (sizeof(n)*8 - 1);

        n = atoi(str);

        for(i=0; i < sizeof(n)*8 ; i++, n <<= 1)
                n&bit ? printf("1") : printf("0");

        return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM