Pages: [1]   Go Down
Author Topic: Quick Q aboout serial.print and strange results.  (Read 318 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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:

Code:
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?
« Last Edit: January 31, 2013, 02:38:43 pm by RicThot » Logged

Offline Offline
Edison Member
*
Karma: 32
Posts: 1394
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Faraday Member
**
Karma: 61
Posts: 2898
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Pages: [1]   Go Up
Jump to: