Convert decimal to binary in C

2020-02-01 03:10发布

I am trying to convert a decimal to binary such as 192 to 11000000. I just need some simple code to do this but the code I have so far doesn't work:

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

Any help would be much appreciated!

标签: c binary decimal
16条回答
可以哭但决不认输i
2楼-- · 2020-02-01 03:39

It looks like this, but be careful, you have to reverse the resulting string :-)

#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);
}
查看更多
Animai°情兽
3楼-- · 2020-02-01 03:39

Convert Decimal to Binary in C Language

#include<stdio.h>
void main()
{
    long int n,n1,m=1,rem,ans=0;
    printf("\nEnter Your Decimal No (between 0 to 1023) :: ");
    scanf("%ld",&n);

    n1=n;
    while(n>0)
    {
        rem=n%2;
        ans=(rem*m)+ans;
        n=n/2;
        m=m*10;
    }

    printf("\nYour Decimal No is   :: %ld",n1);
    printf("\nConvert into Binary No is :: %ld",ans);
}
查看更多
放我归山
4楼-- · 2020-02-01 03:40

Here is the Algorithm to convert Decimal to Binary number

  • Divide the input decimal number by 2 and store the remainder.
  • Store the quotient back to the input number variable.
  • Repeat this process till quotient becomes zero.
  • Equivalent binary number will be the remainders in above process in reverse order.

You can check c program here http://www.techcrashcourse.com/2015/08/c-program-to-convert-decimal-number-binary.html

查看更多
仙女界的扛把子
5楼-- · 2020-02-01 03:40

//C Program to convert Decimal to binary using Stack

#include<stdio.h>

#define max 100

int stack[max],top=-1,i,x;  


void push (int x)
{
  ++top;
  stack [top] = x;
}

int pop ()
{ 
   return stack[top];
}   


void  main()
{
  int num, total = 0,item;
  print f( "Please enter a decimal: ");
  scanf("%d",&num);
  while(num > 0)
  {  
    total = num % 2;
    push(total);
    num /= 2;
  }



 for(i=top;top>-1;top--)
 {     
     item = pop ();
     print f("%d",item);
 }

 }
查看更多
劳资没心,怎么记你
6楼-- · 2020-02-01 03:44

5 digits are not enough for your example (192). Probably you should increase output

查看更多
对你真心纯属浪费
7楼-- · 2020-02-01 03:46

A few days ago, I was searching for fast and portable way of doing sprintf("%d", num). Found this implementation at the page itoa with GCC:

/**
 * 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;
}
查看更多
登录 后发表回答