Go Down

Topic: Quick Q aboout serial.print and strange results. (Read 617 times) previous topic - next topic


Jan 31, 2013, 08:25 pm Last Edit: Jan 31, 2013, 08:38 pm by RicThot Reason: 1
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:

Code: [Select]

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:
Code: [Select]

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

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

Code: [Select]
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.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131