This code sample is not programming at all, is something opposite. I have no idea why would you work with all that "hex" and "binary", which can be only strings, not numbers. I'm not sure you really need to work with strings.
Here is the idea of creation of "binary string". Execute a loop from 0 to
NumberOfBits − 1
, let's say, bit number is
bit
. For each bit, create a
bit mask. For simplicity, let's assume you work with number (not stings, indeed, the function of your signature makes no practical sense). Let's assume you work with
int
:
int mask = 1 << bit;
Then calculate bitwise AND with your test number:
int test = value & mask;
This test will return either zero or not. If it is zero, the test bit in position
bit
is not set (clear, 0), if it is non-zero, it is set (1). Depending on that, write the char '0' or '1' to output.
Please see:
http://www.cplusplus.com/doc/tutorial/operators[
^].
Always work with numbers, not strings representing numbers. You should not "want" to "convert" "hex string" to "binary string". You can
parse "hex string" into number (which cannot be "hex" or "decimal") first. There can be many ways to do it. One of them can be based on using streams:
http://stackoverflow.com/questions/1070497/c-convert-hex-string-to-signed-integer[
^].
—SA