0

I am trying to convert int to binary as string but I can not. Please help me. How to convert integer to binary, please tell me.

Input: 32
Output: 00100000

My code:

#include <stdio.h>
#include <string.h>
char converttobinary(int n)
{
    int i;
    int a[8];
    char op;
    for (i = 0; i < 8; i++)
    {
        a[i] = n % 2;
        n = (n - a[i]) / 2;
    }
    for (i = 7; i >= 0; i--)
    {
        op = strcat(op, a[i]);
    }
    return op;
}
int main()
{
    int n;
    char str;
    n = 254;
    str = converttobinary(n);
    printf("%c", str);
    return 0;

}

5
  • So. what type is op and which values can it have? What is the return type of your function and which values can it return? Commented Jul 10, 2019 at 14:50
  • 2
    op is a single character, so you can't strcat() it. Commented Jul 10, 2019 at 14:51
  • Please tell me specifically. How to modify it. Commented Jul 10, 2019 at 14:53
  • 1
    You need to go back to your textbook or tutorial and study the difference between characters and strings. Commented Jul 10, 2019 at 15:24
  • There are lots of questions with solutions to this problem Commented Jul 10, 2019 at 15:43

3 Answers 3

1

I have tried to modify your solution with minimal changes to make it work. There are elegant solutions to convert Integer to Binary for example using shift operators.

One of the main issue in the code was you were using character instead of character array. i.e char str; instead of char str[SIZE]; Also you were performing string operations on a single character. Additionally, iostream header file is for C++.

There is room for lot of improvements in the solution posted below (I only made your code work with minimal changes).

My suggestion is to make your C basics strong and approach this problem again.

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

void converttobinary(int n, char *op)
{
    int i;
    int a[8];
    for (i = 0; i < 8; i++)
    {
        a[i] = n % 2;
        n = (n - a[i]) / 2;
    }
    for (i = 7; i >= 0; i--)
    {
        op[i]=a[i];
    }

}
int main()
{
    int n,i;
    char str[8];
    n = 8;
    converttobinary(n,str);
    for (i = 7; i >= 0; i--)
    {
        printf(" %d ",str[i]);
    }
    return 0;

}
Sign up to request clarification or add additional context in comments.

1 Comment

When is the string null-terminated? Well, it isn't null-terminated, so it isn't actually a string — by definition, a string is null-terminated. You could use printf("%8.8s\n", str); to safely print the byte array without spaces between the digits.
0
char *rev(char *str)
{
    char *end = str + strlen(str) - 1;
    char *saved = str;
    while(end > str)
    {
        int tmp = *str;
        *str++ = *end;
        *end-- = tmp;
    }
    return saved;
}

char *tobin(char *buff, unsigned long long data)
{
    char *saved = buff;

    while(data)
    {
        *buff++ = (data & 1) + '0';
        data >>= 1;
    }
    *buff = 0;
    return rev(saved);
}

int main()
{

    char x[128];
    unsigned long long z = 0x103;
    printf("%llu is 0b%s\n", z, tobin(x, z));

    return 0;
}

Comments

0

I modify your code a little bit to make what you want, the result of this code with

n = 10

is

00001010

In this code i shift the bits n positions of the imput and compare if there is 1 or 0 in this position and write a '1' if there is a 1 or a '0' if we have a 0.

#include <stdio.h>
void converttobinary(int n, char op[8]){
    int auxiliar = n;
    int i;

    for (i = 0; i < 8; i++) {
        auxiliar = auxiliar >> i;
        if (auxiliar & 1 == 1){
            op[7-i] = '1';
        } else{
            op[7-i] = '0';
        }
        auxiliar = n;
    }

}

int main (void){
    int n = 10;
    int i;
    char op[8];

    converttobinary(n, op);

    for(i = 7; i > -1; i--){
        printf("%c",op[i]);
    }

    return 0;

}

2 Comments

The output 'string' isn't null terminated, so it isn't a string, and you can't safely print it with %s. You could use printf("%8.8s\n", op); to print the byte-array.
@JonathanLeffler thats true i just fix it thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.