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:
- Integer
n
, the one to be converted to binary string (character array). 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')