The value 34 consists of 2 digits. You can separate the two digits into separate variables:
byte num = 34;
byte digit1 = num / 10; // digit1 will be 3
byte digit2 = num % 10; // digit2 will be 4
Now, you just need to convert each digit to a binary representation.
byte b[4];
if(digit >= 8)
{
b[0] = 1; // If digit is 8 or 9, first position is 1
digit -= 8;
}
else
b[0] = 0; // Otherwise, it's 0
// digit is now 7 or less
if(digit >= 4)
{
b[1] = 1; // if digit is 4, 5, 6, or 7, 2nd position is 1
digit -= 4;
}
else
b[1] = 0; // Otherwise, it's 0
// digit is now 3 or less
if(digit >= 2)
{
b[2] = 1; // if digit is 2 or 3, 3rd position is 1
digit -= 2;
}
else
b[2] = 0; // Otherwise, it's 0
// digit is now 0 or 1
b[3] = digit;
There are much shorter ways of doing this, but this works, and is easy to understand.
It should, but I don't think you'll remember in 6 months why you are multiplying by 16 in the first part. Left-shifting by 4 accomplishes the same goal, and is easier to remember the goal later.