Quick Q aboout serial.print and strange results.

Hi guys,

I have what I think must be a pretty basic question for a veteran programmer.
I have a really basic function that fills a 256bytes buffer with simple data:

void init_buffer()
{
  for (int x=0;x<256;x++)
  {
    buff[x] = x;
  }
}

Then, for debugging purposes, I have a small bit of code that reads the buffer and print it to the serial console in rows of 32 HEX values:

Serial.println("Buffer BEGIN:");
  int cnt = 1;
  int i;
  for(i=0;i<256;i++) {
    if (cnt == 32)
    {
      Serial.println("\n");
      cnt = 1;
    }
    Serial.print(buff[i], HEX);
    Serial.print(" ");
    cnt++;
  }
  Serial.println("\nBuffer END\n");

It works, BUT... starting with the 128th value 0x80, it pads the value with Fs on the left:

7C 7D 7E 7F FFFFFF80 FFFFFF81 ...

I'm pretty sure there is a very logical, simple and obvious reason for this, but unfortunately, I don't have a clue about it...

Could anyone shed some lights on this please?

You call it buff in one place and buffer in another. Is this a global?

Typo, I wrote the code by hand... sorry... (fixed by editing 1st post...)

But this is the part where I feel dumb.... my buffer is of type char... I should have created it as an unsigned char.

I ran the same code with a DEC ouput and realized the buffer was going like this: ....125 126 127 -127 -126 -125.....

Sorry... still pretty new to all this and C programming.... data types etc...

Look at the definition of a char on the reference page. It is a signed type, with a range -128 to 127.

I'm a bit concerned that it goes from 127 to -127 there.

And the outcome still doesn't explain the original strange printout.

And the outcome still doesn't explain the original strange printout.

Take a look at the Print class. There are several overloads for the print() method. Look at which one you are actually calling. I think you'll see why you got the results you got.