Unexpected: Serial Print Unsigned Char in Binary Format (Quick Question)

Hello!

Just a quick question: I'm trying to get a byte (declared unsigned char) printed via Serial.print() in binary format. I initialised it as 0x00, so I expected 00000000, but instead, I get 10111111 repeatedly. Does anyone know what causes this? I'm using this byte to record certain events bitwise (8 different events, 1 or 0).

Thank you!

Does anyone know what causes this?

99.999% of the time it is a coding error. Post you code.

Print it in hex or decimal - if it comes out as 0xBF or 191 decimal, the print isn't your problem.

Thanks for replying!

I was hoping someone knew a reason offhand so I didn't have to reprogram it! I'm using a standalone AVR without a bootloader on a circuit board connected via SPI to another AVR. Complicates things a little to reprogram it.

Well my entire code is very long, but I've checked through the entire thing, the only relevant parts are:

unsigned char errors = 0x00; //Global
Serial.println(errors, BIN); //Stuck somewhere in a function

That output looks suspiciously like a signed negative value (-65)

but I've checked through the entire thing, the only relevant parts are:

That is, quite frequently, not the case.

the only relevant parts are:

Doesn't seem likely, does it?

Just went through the entire code again, searching for "errors". Nothing but those two lines!

OK if you say so, then it must be one of those two lines.

I must add that "errors" currently does nothing! I haven't written the code to test for the errors and flip the bits. I'm only printing it as an acknowledgement when I send a command over serial.

Well I guess this isn't going to be as quick as I hoped. :~

I'll make it print HEX and DEC as well and report back!

There's nothing wrong with the two lines of code your provided. I run that code, get the expected output (which is zero 0, not 00000000, that's just the way println() works. It doesn't print leading zeros.)

So, by process of elimination, the problem HAS to be somewhere in the rest of your code. Your reluctance to provide the information necessary to help you find your problem only delays getting useful feedback. If you'd just provided your code in your first post, you very likely would have received some useful feedback already.

I'm betting on a buffer overflow.

Honestly, I didn't think the problem would be with those two lines, since I've used Serial.print() many many times before. I thought perhaps I would be caused by attempting it on an unsigned char specifically.

My code is over a thousand lines long so I hardly think it'll be helpful to post it here.

Thanks AWOL for the helpful suggestion! Since the problem probably doesn't lie with the two lines, I'll just have to test entire code again.

Honestly, I didn't think the problem would be with those two lines

Neither did we.

Crappinni:
My code is over a thousand lines long so I hardly think it'll be helpful to post it here.

I think it's about the only thing that would give us a chance of helping you.

Under Additional Options, Attach: attach your source file.

It's simple.
It's quick.
There are no guessing games.

Oh, and since we appear to be playing a guessing game, I'm betting it's a type conversion error.

Well I'll just start with printing HEX and DEC. Considering it's just 8 bytes for the USART buffer once every few seconds, it's unlikely to be that as well. I'm not sure how I'll test that though.

Thanks for offering to help debug the entire code. The code is actually for an autonomous quadrotor using distributed algorithms of a quaternion attitude estimator and it requires a lot of specific hardware to even run so I'll just try to fix this myself.

Thanks for the replies anyway!