2

192 から 11000000 などの 10 進数を 2 進数に変換しようとしています。これを行うには簡単なコードが必要ですが、これまでのコードは機能しません。

void dectobin(int value, char* output)
{
    int i;
    output[5] = '\0';
    for (i = 4; i >= 0; --i, value >>= 1)
    {
        output[i] = (value & 1) + '0';
    }
}

どんな助けでも大歓迎です!

4

17 に答える 17

4

値は 10 進数ではありません。コンピュータのメモリ内の値はすべてバイナリです。

あなたがやろうとしているのは、特定のベースを使用して int を文字列に変換することです。そのための関数があり、それは と呼ばれitoaます。 http://www.cplusplus.com/reference/cstdlib/itoa/

于 2013-01-11T14:40:00.247 に答える
3

そもそもビット192で表現できない4

192 = 1100 0000これには最小8ビットが必要でした。

これは、 10 進数システムを 2 進数システムに変換する簡単なC プログラムです。

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

int main()  
{  
    long decimal, tempDecimal;  
    char binary[65];  
    int index = 0;  

    /* 
     * Reads decimal number from user 
     */  
    printf("Enter any decimal value : ");  
    scanf("%ld", &decimal);  

    /* Copies decimal value to temp variable */  
    tempDecimal = decimal;  

    while(tempDecimal!=0)  
    {  
        /* Finds decimal%2 and adds to the binary value */  
        binary[index] = (tempDecimal % 2) + '0';  

        tempDecimal /= 2;  
        index++;  
    }  
    binary[index] = '\0';  

    /* Reverse the binary value found */  
    strrev(binary);  

    printf("\nDecimal value = %ld\n", decimal);  
    printf("Binary value of decimal = %s", binary);  

    return 0;  
} 
于 2015-08-24T06:21:38.393 に答える
2

あなたの例(192)には5桁では不十分です。多分増やした方がいいoutput

于 2013-01-11T14:39:44.093 に答える
1

数日前、私は高速でポータブルな方法を探していsprintf("%d", num)ました。この実装は、GCCのページitoaで見つかりました:

/**
 * C++ version 0.4 char* style "itoa":
 * Written by Lukás Chmela
 * Released under GPLv3.

 */
char* itoa(int value, char* result, int base) {
    // check that the base if valid
    if (base < 2 || base > 36) { *result = '\0'; return result; }

    char* ptr = result, *ptr1 = result, tmp_char;
    int tmp_value;

    do {
        tmp_value = value;
        value /= base;
        *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
    } while ( value );

    // Apply negative sign
    if (tmp_value < 0) *ptr++ = '-';
    *ptr-- = '\0';
    while(ptr1 < ptr) {
        tmp_char = *ptr;
        *ptr--= *ptr1;
        *ptr1++ = tmp_char;
    }
    return result;
}
于 2013-01-11T18:43:33.703 に答える
1

10進数を2進数に変換するアルゴリズムは次のとおりです

  • 入力 10 進数を 2 で割り、余りを格納します。
  • 商を入力数値変数に格納します。
  • 商がゼロになるまでこのプロセスを繰り返します。
  • 同等の 2 進数は、逆の順序で上記のプロセスの残りになります。

ここでCプログラムを確認できますhttp://www.techcrashcourse.com/2015/08/c-program-to-convert-decimal-number-binary.html

于 2017-05-13T18:04:31.827 に答える
0

それで...コードの出力をチェックして、それが機能しない理由を理解しましたか?

So iteration 1 of your loop:
value = 192
i = 4
output[i] = (11000000 & 1) + '0' = 0 + 48 = 48 (char `0`)

Iteration 2 of your loop:
value = 96
i = 3
output[i] = (1100000 & 1) + '0' = 0 + 48 = 48 (char `0`)

Iteration 3 of your loop:
value = 48
i = 2
output[i] = (110000 & 1) + '0' = 0 + 48 = 48 (char `0`)

Iteration 4 of your loop:
value = 24
i = 1
output[i] = (11000 & 1) + '0' = 0 + 48 = 48 (char `0`)

Iteration 5 of your loop:
value = 12
i = 0
output[i] = (1100 & 1) + '0' = 0 + 48 = 48 (char `0`)

Final string: "00000"  and you wanted: "11000000"

コードに何か問題がありますか? いいえ。私もあなたは十分に行きませんでした。出力/ループを次のように変更します。

output[8] = '\0';
for (i = 7; i >= 0; --i, value >>= 1)

そして、正しい結果が返されます。

より一般的なアプローチをお勧めします。固定長の文字列を使用しているため、特定の長さの 2 進数に制限されます。次のようなことをしたいかもしれません:

loop while number dividing down is > 0
count number of times we loop
malloc an array the correct length and be returned
于 2013-01-11T15:21:26.193 に答える
0

関数の下で while ループを使用して実行することもできます。私は私の解決策を探していましたが、私が得た解決策は適切ではなかったので、それに応じて実用的なアプローチ(0になるまで2を使用して除算し、リマインダーを配列に保存する)を行い、配列の逆を出力して共有しましたここ

#include <stdio.h>

    int main()
    {
        long long int a,c;
        int i=0,count=0;
        char bol[10000];
        scanf("%lld", &a);
        c = a;
        while(a!=0)
        {
            bol[i] = a%2;
            a = a / 2;
            count++;
            i++;
        }
        if(c==0)
        {
            printf("0");
        }
        else
        {
            for(i=count-1; i>=0; i--)
            {
                printf("%d", bol[i]);
            }
        }
        printf("\n");
        return 0;
    }
于 2013-08-12T04:47:26.887 に答える
0
#include <stdio.h>
#include <stdlib.h>
void bin(int num) {
    int n = num;
    char *s = malloc(sizeof(int) * 8);
    int i, c = 0;
    printf("%d\n", num);

    for (i = sizeof(int) * 8 - 1; i >= 0; i--) {
        n = num >> i;
        *(s + c) = (n & 1) ? '1' : '0';
        c++;
    }
    *(s + c) = NULL;
    printf("%s", s); // or you can also return the string s and then free it whenever needed
}

int main(int argc, char *argv[]) {
    bin(atoi(argv[1]));
    return EXIT_SUCCESS;
}
于 2013-05-01T05:45:56.393 に答える
0

このように見えますが、結果の文字列を逆にする必要があることに注意してください:-)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char output[256]="";

int main()
{
int x= 192;
int n;
n = x;
int r;
do {
r = n % 2;
if (r == 1)
   strcat(output,"1");
else strcat(output,"0");
n = n / 2;
}
while (n > 0);

printf("%s\n",output);
}
于 2013-01-11T14:56:17.233 に答える
-1

おそらく、アルゴリズムを理解することで、必要なものに合わせて独自のコードを作成または変更できるようになります。ただし、192 のバイナリ値を表示するのに十分な char 配列の長さがありません (8 桁のバイナリが必要ですが、コードは 5 桁のバイナリしか与えません)。

アルゴリズムをわかりやすく説明しているページです。

私は C/C++ プログラマーではないので、アルゴリズムの例に基づいた C# コードの貢献を以下に示します。

int I = 0;
int Q = 95;
string B = "";
while (Q != 0)
{
    Debug.Print(I.ToString());
    B += (Q%2);
    Q = Q/2;
    Debug.Print(Q.ToString());
    I++;
}

Debug.Print(B);

すべての Debug.Print は、出力を表示するためだけのものです。

于 2013-01-11T15:03:10.447 に答える
-1
//decimal to binary converter
long int dec2bin(unsigned int decimal_number){
  if (decimal_number == 0)
    return 0;
  else
    return ((decimal_number%2) + 10 * dec2bin(decimal_number/2));
}
于 2018-02-06T10:27:53.063 に答える
-2
int main() 
{ 
 int n, c, k;
 printf("Enter an integer in decimal number system: ");
 scanf("%d", &n);
 printf("%d in binary number system is: ", n);
 for (c = n; c > 0; c = c/2) 
  {
   k = c % 2;//To
   k = (k > 0) ? printf("1") : printf("0");
  }
 getch();
 return 0; 
}
于 2016-10-21T18:24:34.557 に答える