Strange behavior of Serial.print

I'm using an Uno to talk to TI Gas Gauge battery monitors. I'm using Arduino 1.6.2 to write the code.

When I run this code:

char Man_BlockA[33]
char Man_BlockB[33]
char Man_BlockC[33]

void dump_man_blocks() {  // Read and dump all 3 manufacturer blocks
  read_MBA();
  read_MBB();
  read_MBC();
  Serial.println(F("Manufacturer Data Blocks:"));
  Serial.println(F("0....5....1....5....2....5....3.."));
                    for(byte i = 0; i < 33; i++) Serial.print(Man_BlockA[i]);
  Serial.println(); for(byte i = 0; i < 33; i++) Serial.print(Man_BlockB[i]);
  Serial.println(); for(byte i = 0; i < 33; i++) Serial.print(Man_BlockC[i]);
  Serial.println(); Serial.println();
}

I get this output:

Manufacturer Data Blocks:
0....5....1....5....2....5....3..
˜öH9BH
F5D606219FNFW5VCR

Which is what I want, except that some non-printing characters show up and mess with the alignment. Since I don't care about non-printing characters (I'm looking for an ASCII serial number string) I changed it to this:

char Man_BlockA[33]
char Man_BlockB[33]
char Man_BlockC[33]

void dump_man_blocks() {  // Read and dump all 3 manufacturer blocks
  read_MBA();
  read_MBB();
  read_MBC();
  Serial.println(F("Manufacturer Data Blocks:"));
  Serial.println(F("0....5....1....5....2....5....3.."));
                    for(byte i = 0; i < 33; i++) sprintable(Man_BlockA[i]);
  Serial.println(); for(byte i = 0; i < 33; i++) sprintable(Man_BlockB[i]);
  Serial.println(); for(byte i = 0; i < 33; i++) sprintable(Man_BlockC[i]);
  Serial.println(); Serial.println();
}


void sprintable (byte ch) {
  if(ch < 32) {
    Serial.print(F("."));
  }else{
    Serial.print(ch);
  }
}

But now I get this output:

Manufacturer Data Blocks:
0....5....1....5....2....5....3..
152246.72576672..........................
7053685448545049577078708753866782................
.................................

It's outputting decimal ASCII codes instead of characters!

After much head-scratching and a shrug, I tried this:

char Man_BlockA[33]
char Man_BlockB[33]
char Man_BlockC[33]

void dump_man_blocks() {  // Read and dump all 3 manufacturer blocks
  read_MBA();
  read_MBB();
  read_MBC();
  Serial.println(F("Manufacturer Data Blocks:"));
  Serial.println(F("0....5....1....5....2....5....3.."));
                    for(byte i = 0; i < 33; i++) sprintable(Man_BlockA[i]);
  Serial.println(); for(byte i = 0; i < 33; i++) sprintable(Man_BlockB[i]);
  Serial.println(); for(byte i = 0; i < 33; i++) sprintable(Man_BlockC[i]);
  Serial.println(); Serial.println();
}


void sprintable (byte ch) {
  if(ch < 32) {
    Serial.print(F("."));
  }else{
    Serial.write(ch);
  }
}

All I did was change Serial.print(ch) to Serial.write(ch). And now I get the output I'm after:

Manufacturer Data Blocks:
0....5....1....5....2....5....3..
˜ö.H9BH..........................
F5D606219FNFW5VCR................
.................................

So, I guess, problem solved. But inquiring minds want to know why that second version doesn't work? What am I doing to Serial.print that makes it output the numeric value instead of the actual character? (I might want that behavior some other time :slight_smile:

So, I guess, problem solved. But inquiring minds want to know why that second version doesn't work? What am I doing to Serial.print that makes it output the numeric value instead of the actual character? (I might want that behavior some other time :slight_smile:

void sprintable ([color=red]byte[/color] ch) {

try with

void sprintable ([color=green]char[/color] ch) {

Thanks, that does it. And now reading the Serial.print reference with new eyes, it makes sense.