converting integer into 4 bit binary

decimal to binary
binary calculator
convert decimal fraction to binary
decimal to 8-bit binary
binary representation of numbers
binary chart
negative decimal to binary
decimal to binary code

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

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

Binary Representation of numbers, For instance, to represent the positive integer one hundred and twenty-five as a decimal Convert 37 to binary, shift it left by one and convert back to decimal. In hexadecimal notation, 4 bits (a nibble) are represented by a single digit. How to convert decimal to binary Conversion steps: Divide the number by 2. Get the integer quotient for the next iteration. Get the remainder for the binary digit. Repeat the steps until the quotient is equal to 0. Example #1. Convert 13 10 to binary:

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|.

Convert an integer to binary in Java, In this post, we will see how to convert an integer to a 32-bit binary string in Java. 4. 5. 6. 7. 8. 9. 10. 11. 12. class Main. {. public static String toBinary(int x) {. I'm trying to convert an integer 10 into the binary number 1010. This code attempts it, but I get a segfault on the strcat(): int int_to_bin(int k) { char *bin; bin = (char *)malloc(sizeo

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;
}

Is there a function in C that converts an integer into bits?, char *s = malloc(nbits+1); // +1 for '\0' terminator. s[nbits] There's no need to convert an integer to bits. int n = 12345; // the number we'll print out in binary. In order to convert binary to decimal, basic knowledge on how to read a binary number might help. As mentioned above, in the positional system of binary, each bit (binary digit) is a power of 2. This means that every binary number could be represented as powers of 2, with the rightmost one being in the position of 2 0.

Decimal to Binary Converter, How to convert decimal to binary. Conversion steps: Divide the number by 2. Get the integer quotient for the next iteration. Get the remainder for the binary digit. Note: The program works only with binary numbers in the range of integers. In case you want to work with long binary numbers like 20 bits or 30 bit, you can use string variable to store the binary numbers. Below is a similar program which uses string variable instead of integers to store binary value:

Binary Number Representations, What is the 32-bit unsigned binary integer representation for the decimal integer 4. Convert to hex: 0000 0000 0000 0001 0101 0001 1001 0100. 00015194� '{:032b}'.format(n) where n is an integer. If the binary representation is greater than 32 digits it will expand as necessary: If the binary representation is greater than 32 digits it will expand as necessary:

Integer (computer science), In computer science, an integer is a datum of integral data type, a data type that represents some range of mathematical integers. Integral data types may be of different sizes and may or may not be allowed to contain negative values. Integers are commonly represented in a computer as a group of binary digits (bits ). integer values to bit patterns are sometimes used, for example � @mVChr: str.format() is the wrong tool anyway, you would use format(i, 'b') instead. Take into account that that also gives you padding and alignment options though; format(i, '016b') to format to a 16-bit zero-padded binary number.

Comments
  • 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.
  • thank you, I made this with the help of Ajay Brahmakshatriya, which almost had the same answer as yours.