sprintf is not running correctly on my 3.6, Thanks.

Status
Not open for further replies.

laptophead

Well-known member
Greetings community,

I just learned to use sprintf and what a blessing, I am saving on tone of code.

Here is a function I am trying to simplify, lots of data, I included the "old way" and the sprintf at the bottom

HTML:
void Show_Inc_String (char Channel)

{
  Serial.print (Channel);
  Serial.print(" Byte 0:");
  Serial.print(mybuffer_A[0], HEX);
  Serial.print(" By1:");
  Serial.print(mybuffer_A[1], HEX);
  Serial.print(" By2:");
  Serial.print(mybuffer_A[2], HEX);
  Serial.print(" By3:");
  Serial.print(mybuffer_A[3], HEX);
  Serial.print(" By4:");
  Serial.print(mybuffer_A[4], HEX);
  Serial.print(" By5:");
  Serial.print(mybuffer_A[5], HEX);
  Serial.print(" By6:");
  Serial.print(mybuffer_A[6], HEX);
  Serial.print(" By7:");
  Serial.print(mybuffer_A[7], HEX);

  Serial.print(" By8:");
  Serial.print(mybuffer_A[8], HEX);
  Serial.print(" By9:");
  Serial.print(mybuffer_A[9], HEX);
  Serial.print(" By10:");
  Serial.print(mybuffer_A[10], HEX);
  Serial.print(" By11:");
  Serial.print(mybuffer_A[11], HEX);
  Serial.print(" By12:");
  Serial.print(mybuffer_A[12], HEX);
  Serial.print(" By13:");
  Serial.print(mybuffer_A[13], HEX);
  Serial.print(" By14:");
  Serial.print(mybuffer_A[14], HEX);
  Serial.print(" By15:");
  Serial.print(mybuffer_A[15], HEX);
  Serial.print(" By16:");
  Serial.print(mybuffer_A[16], HEX);
  Serial.print(" By17:");
  Serial.println(mybuffer_A[17], HEX);
  
  sprintf(data,"sprintf %c BYTE 0:%X 1:%X 2:%X 3:%X 4:%X 5:%X 6:%X 7:%X 8:%X 9:%X 10:%X 11:%X 12:%X 13:%X 14:%X 15:%X 16:%X 17:%X  ",
  Channel, mybuffer_A[0], mybuffer_A[1], mybuffer_A[2], mybuffer_A[3], mybuffer_A[4], mybuffer_A[5], 
  mybuffer_A[6], mybuffer_A[7],mybuffer_A[8], mybuffer_A[9], mybuffer_A[10], mybuffer_A[11]), 
  mybuffer_A[12], mybuffer_A[13], mybuffer_A[14], mybuffer_A[15], mybuffer_A[16], mybuffer_A[17];
  Serial.println(data);
}

I would expect to get the same results, but I get:

A Byte 0:0 By1:81 By2:1 By3:0 By4:C0 By5:C9 By6:1 By7:0 By8:0 By9:0 By10:43 By11:12 By12:1F By13:0 By14:0 By15:0 By16:0 By17:0
sprintf A BYTE 0:0 1:81 2:1 3:0 4:C0 5:C9 6:1 7:0 8:0 9:0 10:43 11:12 12:0 13:0 14:1FFF1A48 15:0 16:0 17:0


All fine, but the byte 14 is totally off,

I declared data as
char data[1000]; global value
What am I missing>? Thanks
Mitch
 
Last edited:
Please provide a complete program that anyone can copy into Arduino and get the same result you're seeing.

Don't make us have to guess details, like the declaration of "mybuffer_A".
 
You have a typo here: _A[11]),

Admittedly, a hard one to spot unless you watch warnings.
 
You have a typo here: _A[11]),

Admittedly, a hard one to spot unless you watch warnings.

Indeed, that is HARD to spot - even with your notice - easy to run right over the extra closing ')'!

Which is the issue when compounding output like that with printf() or sprint(), things get out of order or shifted.

Before posting code or when anomalies or errors show up it is good in the IDE { or if so equipped the editor of your choice when not the IDE }

>> Do a CODE FORMAT - in the IDE this is :: Ctrl+T

Doing that here triggers on the closing brace and at least partially draws attention to the point of the problem where it does this to that line - doing an INDENT based on the closing paren:
Code:
  Serial.println(mybuffer_A[17], HEX);

  sprintf(data, "sprintf %c BYTE 0:%X 1:%X 2:%X 3:%X 4:%X 5:%X 6:%X 7:%X 8:%X 9:%X 10:%X 11:%X 12:%X 13:%X 14:%X 15:%X 16:%X 17:%X  ",
          Channel, mybuffer_A[0], mybuffer_A[1], mybuffer_A[2], mybuffer_A[3], mybuffer_A[4], mybuffer_A[5],
          mybuffer_A[6], mybuffer_A[7], mybuffer_A[8], mybuffer_A[9], mybuffer_A[10], mybuffer_A[11]),
                     mybuffer_A[12], mybuffer_A[13], mybuffer_A[14], mybuffer_A[15], mybuffer_A[16], mybuffer_A[17];
  Serial.println(data);
}

Of course scanning the console output for warnings is good - but they can cascade uselessly after the true error and the "Ctrl+T" can make that stand out.
 
A[11]), was the mistake, thanks every one.

Embarrassingly simple, sorry for the trouble.

Paul:
For the future I will post more definitions and code. In this case the data comes from a motor controller, hard to duplicate the problem...

Thanks
 
Status
Not open for further replies.
Back
Top