I am trying to convert an array of characters to binary. I've spent an hour now perusing SO posts, but few do quite what I need and none of them has worked for me.
void formatCallSign(unsigned char *callsign) {
unsigned char new[7];
printf("original is %s",callsign);
for (int j = 0; j < 6; j++) {
printf("Hex of this is %x", callsign[j]); // this correctly prints out the original hex value
char binary[9];
binary[0] = ( (callsign[j] & (1 << 7)) ? '1' : '0' );
binary[1] = ( (callsign[j] & (1 << 6)) ? '1' : '0' );
binary[2] = ( (callsign[j] & (1 << 5)) ? '1' : '0' );
binary[3] = ( (callsign[j] & (1 << 4)) ? '1' : '0' );
binary[4] = ( (callsign[j] & (1 << 3)) ? '1' : '0' );
binary[5] = ( (callsign[j] & (1 << 2)) ? '1' : '0' );
binary[6] = ( (callsign[j] & (1 << 1)) ? '1' : '0' );
binary[7] = ( (callsign[j] & (1 << 0)) ? '1' : '0' );
printf("Binary of %c is: %x\r\n", callsign[j], binary); // print hex representation of character
}
}
Essentially, the input to this function is a 7-character string (char array). I was also using a nested for loop, but the compiler was generating infinite loop errors that made no sense, so I rewrote it like this.
I need to then get the binary representation of that character and shift do a left bitwise shift by 1 (not always but 95% of the time, I can add the logic later). After the bitshift, I need the hex value.
Getting the binary value has proved elusive. In a prior project, I was never able to do it properly, and could only get the hex value. In this case, I can also get the hax value in the second printf statement, but I can't do a bitwise shift with the hex value, as I need to shift the individual bits.
Yet, when I run the program, it always says Binary of [char] is: 9A
. Always 9A, which is not the hex rep. of any of the characters in the string I pass into it.
My thought is to use sprintf
and pass in the array and use %x
to get the hex representation of it. Right now, it prints to the console, but I would actually store that in the new array, which is unused in this snippet right now. Is it possible to do this somehow? The code to get the binary doesn't seem to work (based on the second part of this answer).
(To clarify, my 2nd and 3rd printf
statements in this snippet should output the same thing. Only the 2nd one works. The reason for drawing it out like that is so I can do bitwise operations and then use a sprintf to put it back into a coherent single value and get the hex from that.)
Many questions address this topic, but few have been helpful because I am not interested in printing the binary out to the console. Rather, I need to store the binary in a variable, do bitwise operations with it, then get a hex representation of the final binary. Right now, I can successfully get a hex representation of the original character, but that is not helpful.
My only other idea is to manually define the binary values of every hex number and then use that as a "lookup table", but this seems inefficient.
To test this, all else that is required is this in main
:
unsigned char dest[8] = "CQ ";
formatCallSign(&dest);