I have a one byte char array consisting of a binary value and I am trying to split it into a two-dimensional int array (low nybble and high nybble). This is my code:
int nybbles[2][4]; //[0][] is low nybble, [1][] is high nybble.
for (int i = 0; i < 4; i++) {
nybbles[0][i] = (int)binarr[i];
nybbles[1][i] = (int)binarr[4 + i];
printf("%c%d ", binarr[i], nybbles[0][i]);
printf("%c%d\n", binarr[4 + i], nybbles[1][i]);
}
The output of this is:
048
149
048
048
149
048
048
048
I can easily fix this by adding a "- 48" to the end of both lines of code, as such:
nybbles[0][i] = (int)binarr[i] - 48;
nybbles[1][i] = (int)binarr[4 + i] - 48;
However, I see this as a very brute force solution. Why does this problem exist anyway? Are there better fixes than mine?