Output problem with printlnf

This seems like such a trivial problem, but this code doesn’t work:

if (s.Debug) { Serial.printlnf(“Starting UDP on Port %s .”, String(s.UDPPort, DEC); }

The output is:
image

//In the class declaration for “s”…

int UDPPort = 8023;

The “s” variable is a class. The s.Debug is a bool and works fine because the console shows the output. The s.UDPPort is an int that contains a default value upon initialization of 8023. I’ve tried different variations of the string such as creating a string variable, setting it to String() like above… but nothing has worked. I have a few places where this is happening in the printlnf functions. I resolve the one where I’m outputting a time by using:

if (s.Debug) { Serial.printlnf("DSBcmd message received at %s: ", Time.timeStr().c_str()); }

Any ideas how to fix? Is this a syntax problem?

Try Serial.printf();

Thanks, just tested it. printf produces the same output text "?z".

The problem is that String cannot be rendered as a variable argument to sprintf and its relatives like Serial.printlnf without a cast:

Serial.printlnf(“Starting UDP on Port %s .”, (const char *) String(s.UDPPort, DEC);

However this is a better way to do it:

Serial.printlnf(“Starting UDP on Port %d .”, s.UDPPort);
3 Likes

Thank you very much. Now I know.