I have a sprintf command that needs to always contain 4 digits.
TargetVAR = 500; //This is my example variable that can be 0-9999
My command is: // sprintf (ReadyData, "A1%4d", TargetVAR);
Serial.println(ReadyData);
This puts the variable into ReadData with a space at the front if the value is less than 4 digits. But I need that space to be a zero.
So the outcome is always for example 0050 or 0500 or 0013.
I should add that the A1 at the beginning is used as a target address for it's destination
The reason is the Java code that receives that data reads it as 4 digits and a space messes it up.
But, odd thing occurring that I cannot figure. It's in my HTML/Java coding, so you may not be interested in here.
The assembled data I send to my browser from my Mega + ESP8266 arrives and I print it out in HTML. That printout shows that the data arrives in the correct format.
My Java then decodes it:
function processReceivedCommand(evt) { // Incoming Websockets data
document.getElementById('rd').innerHTML = evt.data; // Print data is it correct?
if ((evt.data[0] =='A') & (evt.data[1] =='1')) { do this }
This takes the first two address characters and if correct, I then decode the following 4 numbers as the value....
OK. My code works correctly. I have checked what arrives and it's right.
The issue is with the gauges Java applet that decodes that data. Thanks for your help
But, the numbers are incorrect somehow. A few variable examples...
339 comes out as 3309
210 comes out as 2100
107 comes out as 1007
617 comes out as 6107
1023 comes out as 10203
I am not familiar with java, so can't comment on the code itself, but the numbers looks as if the sum of the first three digits is getting multiplied by 10 before adding the last digit.
I've never seen Java automatically convert a string to a number like that. Javascript, on the other hand, does. Are you using Java or Javascript? They're two completely different languages (despite the similar names).