0

I'm struggling to adapt to C after programming in Java for some time and I need help. What I'm looking for is a method that takes following input:

  1. Integer n, the one to be converted to binary string (character array).
  2. Integer length, which defines the length of the string (positions from the left not filled with the binary numbers are going to be set to default 0).

    //Here's some quick code in Java to get a better understanding of what I'm looking for:
    
    public static String convertToBinary(int length, int n) {
      return String.format("%1$" + bit + "s", Integer.toBinaryString(value)).replace(' ', '0');
    }
    
    System.out.println(convertToBinary(8,1));
    
    // OUTPUT:
    
    00000001 (not just 1 or 01)
    

Any hints on what the equivalent of this would be in C? Also, could you provide me with an example of how the resulting binary string should be returned?

(not a duplicate, since what I'm looking for is '00000001', not simply '1')

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
P. Lance
  • 179
  • 2
  • 13

2 Answers2

0

The C standard library does not contain an equivalent function to Integer.toBinaryString(). The good news is, writing such a function won't be too complicated, and if you're in the process of learning C, this problem is fairly ideal for learning how to use the bitwise operators.

You'll want to consult an existing tutorial or manual for all the details, but here are a few examples of the sort of things that would be useful for this or similar tasks. All numbers are unsigned integers in these examples.

n >> m shifts all bits in n right by m steps, and fills in zeros on the left side. So if n = 13 (1101 in binary), n >> 1 would be 6 (i.e. 110), and n >> 2 would be 3 (i.e. 11).

n << m does the same thing, but shifting left. 3 << 2 == 12. This is equivalent to multiplying n by 2 to the power of m. (If it isn't obvious why that is, you'll want to think about how binary numbers are represented for awhile until you understand it clearly; it'll make things easier if you have an intuitive understanding of that property.)

n & m evaluates to a number such that each bit of the result is 1 if and only if it's 1 in both n and m. e.g. 12 & 5 == 4, (1100, 0101, and 0100 being the respective representations of 12, 5, and 4).

So putting those together, n & (1 << i) will be nonzero if and only if bit i is set: 1 obviously only has a single bit set, 1 << i moves it to the appropriate position, and n & (1 << i) checks if that position also has a 1 bit for n. (keeping in mind that the rightmost/least significant bit is bit 0, not bit 1.) So using that, it's a simple matter of checking each bit individually to see if it's 1 or 0, and you have your binary conversion function.

Ray
  • 1,706
  • 22
  • 30
-1

like this:

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

char *convertToBinary(int length, int n) {
    unsigned num = (unsigned)n;
    int n_bit = CHAR_BIT * sizeof(num);
    if(length > n_bit){
        fprintf(stderr, "specified length  greater than maximum length.\n");
        length = n_bit;//or expand size?
    }

    char *bin = malloc(n_bit + 1);//static char bin[CHAR_BIT * sizeof(num)+1]; If you change, memmove(-->return p;), free is not necessary.
    memset(bin, '0', n_bit);
    bin[n_bit] = 0;

    char *p = bin + n_bit;
    do {
        *--p = "01"[num & 1];
        num >>= 1;
    }while(num);

    int bits = bin + n_bit - p;
    if(bits < length){
        p -= length - bits;
        return memmove(bin, p, length + 1);
    } else if(bits > length){
        fprintf(stderr, "Specified length is not enough.(%s but length is %d)\n", p, length);
        return memmove(bin, p, bits+1);//or cut off
/*
        free(bin);
        return ""; or return NULL;
*/
    }// else if(bits == length)
    return bin;
}

int main(void){
    char *sbin = convertToBinary(8, 1);
    puts(sbin);
    free(sbin);

    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70