dotJason:
OK Guys, I knew I wouldn't understand. So the unit that sends the data sends it to me as hex.
No it doesn't. It sends it to you as a sequence of binary bits.
Hex, decimal, roman numerals, sumerian petrogyphs - these are all different ways of representing a number as text. When you write a text file for a compiler to compile, of course if it includes numbers then you need some way of writing those numbers down for the compiler to read. But once your source code has been compiled, that's not what is going on.
Internally, a computer holds numbers as binary values - as a set of 8 (or 16, or 32) flip-flops that can be on or off. In a .cpp file, we write 0x3e. On the physical device, this will correspond to 8 bits of circuitry that are off,off,on,on,on,on,on,off . When a serial connection sends a number from place to place, it sends a sequence of 8 bits one at a time over the wire. That is how the unit "sends" the number.
C++ provides bit fiddling operators: ~ & | ^ << >> >>> that manipulate numbers in ways that pretty much directly correspond to the ways that computers work with numbers. That is: the compile down to simple sequences of machine language. For many purposes, it makes sense to think of 0x3E not as the number 62, but as an array of 8 individual bits.
In addition to decimal, hexadecimal, and octal, C++ also provides a way of writing down 63 in binary: 0b00111110. This makes life easy when you are working with "numbers" simply as sets of bits.
So. given the number 63, how do we work out if the 'wednesday' bit is set?
Well, we do a bitwise and with the value 0b00000100 - aka 4. If the result of that bitwise is nonzero, then the 'wednesday' bit is set. We use the C++ language feature that the if() statment treats nonzero as true, zero as false, and write:
if(dow & 4) { /* wednesday! */ }
if(dow & 0x04) { /* wednesday! */ }
if(dow & 0b00000100) { /* wednesday! */ }
The values for the days are 1,2,4,8,16,32,64,128 - but using decimal obscures what's really going on.
if(dow & 1) { /* monday! */ }
if(dow & 2) { /* tuesday! */ }
if(dow & 4) { /* wednesday! */ }
A better choice might be to use binary:
if(dow & 0b0000001) { /* monday! */ }
if(dow & 0b0000010) { /* tuesday! */ }
if(dow & 0b0000100) { /* wednesday! */ }
But an even better way might be to compute the values using the 'left shift' operator << . To get the individual bits, we left shift the bit-pattern 0b00000001 by the offset that we need
if(dow & (1<<0)) { /* monday! */ }
if(dow & (1<<1)) { /* tuesday! */ }
if(dow & (1<<2)) { /* wednesday! */ }
In that example, the compiler will precompute these values and directly substitute in 1,2,4 and so on in the if/else chain above. But we can also use a loop and compute the numbers as we go:
static char *dayName[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
for(int day = 0; day < 7; day++) {
if(dow & (1<<day)) { Serial.println(dayName[i]);}
}
This allows us to do more complex things. For instance, you could have an array of LED pin numbers and set the pins for the days.