Characters in sprintf

I am trying to use sprintf to print characters on the screen. While searching around on C++ forums I found out that %c allows sprintf to use characters.

Is this the proper coding for such a situation? :

char switch[2] = "On"
....
char buffer[128];
sprintf(buffer,"<script>document.getElementById(\"SwitchState\").innerHTML=%c;</script>", switch);
client.println(buffer);

It doesn’t seem to work properly. The document.getElementById doesn’t complete its purpose of replacing SwitchState.

If there is a better way to use sprintf to print letters through a buffer. Please let me know.

milesmiles902:
I am trying to use sprintf to print characters on the screen. While searching around on C++ forums I found out that %c allows sprintf to use characters.

Is this the proper coding for such a situation? :

char switch[2] = "On"


char buffer[128];
sprintf(buffer,“”, switch);
client.println(buffer);




It doesn't seem to work properly. The document.getElementById doesn't complete its purpose of replacing SwitchState.

If there is a better way to use sprintf to print letters through a buffer. Please let me know.

That’s because switch is an array, and you are passing the pointer to the array, so %c is taking the bottom byte of where buffer is located, and putting it directly into the output. You would typically want to use %s to print a null terminated string. Note, in your declaration to switch, you want to use 3 bytes, to include the trailing null. Finally, switch is a C/C++ keyword.

So, you would want something like:

char switch_on_off[3] = "On";
....
char buffer[128];
sprintf(buffer,"<script>document.getElementById(\"SwitchState\").innerHTML=%s;</script>", switch_on_off);
client.println(buffer);

Sprintf is a big hammer, and it pulls in a lot of code. If you are just doing strings, it may be simpler to use strcpy and strcat:

char switch_on_off[3] = "On";
...
char buffer[128];
strcpy (buffer, "<script>document.getElementByld(\"SwitchState\").innerHTML=");
strcat (buffer, switch_on_off);
strcat (buffer, ";</script>");
client.println(buffer);

Now, if you normally have switch as a boolean, you can do:

bool switch_value = false;
...
char buffer[128];
strcpy (buffer, "<script>document.getElementByld(\"SwitchState\").innerHTML=");
strcat (buffer, (switch_value) ? "On" : "Off");
strcat (buffer, ";</script>");
client.println(buffer);

If speed was critical, you could optimize it further by keeping track of where the end of the string is, and use strcpy/memcpy to copy the string there, rather than having to search for the null byte. However, since you are building up a HTML document, and presumably sending it over the wire, speed is probably not critical at this point.

MichaelMeissner:
That’s because switch is an array, and you are passing the pointer to the array, so %c is taking the bottom byte of where buffer is located, and putting it directly into the output. You would typically want to use %s to print a null terminated string. Note, in your declaration to switch, you want to use 3 bytes, to include the trailing null. Finally, switch is a C/C++ keyword.

So, you would want something like:

char switch_on_off[3] = "On";


char buffer[128];
sprintf(buffer,“”, switch_on_off);
client.println(buffer);




Sprintf is a big hammer, and it pulls in a lot of code. If you are just doing strings, it may be simpler to use strcpy and strcat:



char switch_on_off[3] = “On”;

char buffer[128];
strcpy (buffer, “”);
client.println(buffer);




Now, if you normally have switch as a boolean, you can do:



bool switch_value = false;

char buffer[128];
strcpy (buffer, “”);
client.println(buffer);




If speed was critical, you could optimize it further by keeping track of where the end of the string is, and use strcpy/memcpy to copy the string there, rather than having to search for the null byte. However, since you are building up a HTML document, and presumably sending it over the wire, speed is probably not critical at this point.

Thanks for helping. I think as I get more advanced I will probably apply your second and third posts to my code. They seem quite useful when in a memory crunch.

I think I have everything correct compared to your first code post. Would you be willing to take a look at it?

IDE Code:

char InternalTemperature[3] = "On";
...
char buffer[128];
sprintf(buffer,"<script>document.getElementById(\"Current_Internal_Temp\").innerHTML=%s;</script>", InternalTemperature);
client.println(buffer);

Java:

document.getElementById("Current_Internal_Temp").innerHTML=0;

HTML reference:

<span id="Current_Internal_Temp">Loading...</span>
...
<script type="text/jscript" src="CIT.js">

The HTML in reference is supposed to change to On, rather than Loading. I previously got it to work with data values (so I am sure locations are correct), but now want to apply the same code towards showing if a relay is On or Off. Anything helps.

When you're developing nontrivial syntax using sprintf(buffer, ....); it definitely helps to print it to the SerialMonitor to see what you ended up with, ie use Serial.print(buffer); in addition to client.print(buffer);

oric_dan:
When you’re developing nontrivial syntax using sprintf(buffer, …); it definitely helps to print it to the SerialMonitor to see what you ended up with, ie use Serial.print(buffer); in addition to client.print(buffer);

Alright. That is a simple, but great idea. It helped me troubleshoot the issue.

In this code:

char switch_on_off[3] = "On";
....
char buffer[128];
sprintf(buffer,"<script>document.getElementById(\"SwitchState\").innerHTML=%s;</script>", switch_on_off);
client.println(buffer);

The “Loading” in my html, only changes when I use %d. Not %s or %c. It prints to the serial monitor for both the buffer and switch_on_off. Correctly, but with the document.getElementById.

Is there any reason that my html wouldn’t change for %s or %c?

bump