2

I have the following code to convert an integer into a binary representation for each integer from 0 to 15, thus I need 4-bit representation of an integer.

the code it works fine but the problem is the length of the binary is not correct, so when given 1 as int it returns 1 as output instead of 0001. for 2 it should return 0010 but instead, it returns 10.

How can I change this code so it returns the correct representation? Using printf %04d is oki when printing the results, but just for printing because the actual value is still different.

I was trying to make another method that gets an integer convert it into a string and then depending on its length add 0 before it until the length is 4.

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

int convert(int dec)
{
    if (dec == 0)
    {
        //printf("Base c\n");
        return 0;
    }
    else
    {
        //printf("Rec call\n");
        return (dec % 2) + 10 * convert(dec / 2);
    }
}

int main(int argc, char *argv[]) 
{
    // argc is number of arguments given including a.out in command line
    // argv is a list of string containing command line arguments

    int v = atoi(argv[2]);
    printf("the number is:%d \n", v);


    int bin = 0;

    if(v >= 0 && v <= 15){
        printf("Correct input \n");
        bin = convert(v);
        printf("Binary is: %04d \n", bin);
        printf("Binary is: %d \n", bin);

    }
    else{
        printf("Inorrect input, number cant be accepted");
    }

What I need this method to do: 1. given an integer 2. return 4-bit representation of this integer, not sure if it should be an int or string. 3. for example convert(2) should return 0010, 6 returns 110, and I want this to be 0110 and so on. the method convert was supposed to do that for me. I hope I am clear about what I what to happen.

this should return for:

1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
and so on
15 1111
0

3 Answers 3

2

Your requirements are very unclear but from the comments I think I have gathered what you require. According to my suggestion, you need to change the function to return a string instead of an int.

You need to pass an argument in which the string should be returned. So the function will be -

char * convert(int dec, char *output) {
    output[4] = '\0';
    output[3] = (dec & 1) + '0';
    output[2] = ((dec >> 1) & 1) + '0';
    output[1] = ((dec >> 2) & 1) + '0';
    output[0] = ((dec >> 3) & 1) + '0';
    return output;
}

This function can be used as

 char binary[5];
 convert(15, binary);
 printf("%s", binary);

Demo: Ideone

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

1 Comment

thanks, this is exactly what I need, now when I have the number 1 it returns 0001 and so on. I don't know why U guys couldn't get this from my question I thought I was pretty clear about what I want to happen here. anyways thanks again now I can go further with my assignment. I accept your answer.
1

Initialize a character array of size 5, ex: char arr[5] and mark arr[4] ='\0', rest of the slots as 0 and then store the LSB into the 4th array slot & MSB near the 1st array slot. Print it using %s format specifier.

Initially the array will look like |0|0|0|0|\0|. Let's say that your input is 5 and the output you receive is (in form of int) 101, then after inserting the output into the array will look like |0|1|0|1|\0|.

1 Comment

thank you, I made this with the help of Ajay Brahmakshatriya, which almost had the same answer as yours.
0

My attempt

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



int convert(int dec)
{
    if (dec == 0)
    {
        //printf("Base c\n");
        return 0;
    }
    else
    {
        //printf("Rec call\n");
        return (dec % 2) + 10 * convert(dec / 2);
    }
}
int main()
{
 int binary = 10;
 binary = convert(binary);
 printf("%d", binary);
 return 0;
}

Comments

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.