How to lay out a Spintf command correctly

Hello

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.

Advice? Thanks

How do I achieve or format that?

"%04d"

int TargetVAR = 50;  
char ReadyData[10];

void setup()
{
    Serial.begin(115200);
    sprintf(ReadyData, "A1%04d", TargetVAR);
    Serial.println(ReadyData);
}

void loop()
{

}

Try this with different numbers of different lengths.

Yes, I finally sussed it out...

sprintf (ReadyData, "A1%04i", TargetVAR);

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....

value: ((evt.data[2]*1000) + (evt.data[3]*100) + (evt.data[4]*10) + (evt.data[5])),

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

Not sure where it is getting this extra digit from? (always second from right)

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

Can anyone throw light on what might cause this issue:

When sending data to my gauges code in Java I get two different results...

value: 207, This as an example, tells the gauge to show 207 and it does this correctly.

However..

evt.data[2] = 0
evt.data[3] = 2
evt.data[4] = 0
evt.data[5] = 7

value: ((evt.data[2]*1000) + (evt.data[3]*100) + (evt.data[4]*10) + (evt.data[5])),

then the gauge shows 2007. If I check the individual values of evt.data, they are correct. I don't get it

This takes the first two address characters and if correct, I then decode the following 4 numbers as the value....

value: ((evt.data[2]*1000) + (evt.data[3]*100) + (evt.data[4]*10) + (evt.data[5])),

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.

value: ((evt.data[2]*1000) + (evt.data[3]*100) + (evt.data[4]*10) + (evt.data[5])),

Could it be that evt.data[5] is handled as if it were a string, and because of that the content of value also becomes a string?

What happens if you try

evt.data[5]*1

(to force the result into an integer), instead of

evt.data[5]

?

This is what I think is happening (when the value is 1234):

data[2] * 1000 = 1000
data[3] *  100 =  200
data[4] *   10 =   30
                 ---- +
Total:           1230

data[5] = "4"

1230 + "4" = "12304"

Thank you for the replies. I realise this is off the beaten track for the Arduino forum.

I will try that.

If I print to the screen the individual variables, they are correct. That is what I find odd.

Erik, you legend. That is it. You need to multiply the last digit by 1. Instantly fixed it.

Thank you for the advice

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).

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.