View previous topic :: View next topic |
Author |
Message |
JAM2014
Joined: 24 Apr 2014 Posts: 138
|
Printf formatting.... |
Posted: Wed Jun 23, 2021 2:51 pm |
|
|
Hi All,
I have an signed int8 variable that varies from -10 to +10. I'd like to display the value exactly this way using the printf function. Is there a format specifier that will allow me to do this in a single line? Currently, I need to do this in two lines depending on whether the value is less than 0 or not.
Code: |
if (UTCOffset < 0){
sprintf(TempTimeStr,"PageSetup.txtUTCOffset.txt=\"%03d\"", UTCOffset); SendNextionValue(TempTimeStr);
}
else{
sprintf(TempTimeStr,"PageSetup.txtUTCOffset.txt=\"+%02d\"", UTCOffset); SendNextionValue(TempTimeStr);
}
|
This is not a huge deal except I have other similar variables that I'd like to do in the same way, and the code is getting a bit 'messy'!
Edit: Changed description of variable type!
Thanks,
Jack
Last edited by JAM2014 on Wed Jun 23, 2021 5:51 pm; edited 1 time in total |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9236 Location: Greensville,Ontario
|
|
Posted: Wed Jun 23, 2021 5:24 pm |
|
|
hmmm...
Quote: | I have an unsigned int8 variable that varies from -10 to +10. |
me thinks you have a SIGNED int8 variable... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19534
|
|
Posted: Wed Jun 23, 2021 11:55 pm |
|
|
Yes.
However the problem he is having is down to CCS missing one of the standard
C format flags.
By default a number displays a leading - if negative, but nothing if positive.
There was a flag defined in K&R, that + could be used in front of the
width specifier, and if this existed, the formatter would display + if positive
and - if negative. This though is not supported in CCS.
Suggestion. Create a function to do it. So:
Code: |
char * string_with_sign(signed int8 val)
{
static char buffer[4]; //remember need to be space for NULL as well
//also must be static so can be returned
if (val< 0)
{
sprintf(buffer,"%03d", val);
}
else
{
sprintf(buffer,"+%02d", val);
}
return buffer;
}
//Then when you want to print the value, use this and %s
sprintf(TempTimeStr,"PageSetup.txtUTCOffset.txt=\"%s\"", string_with_sign(UTCOffset)); SendNextionValue(TempTimeStr);
|
Now this can then be used for other values. It just generates a 3 character
'string' containing the number formatted as required. |
|
|
JAM2014
Joined: 24 Apr 2014 Posts: 138
|
|
Posted: Fri Jun 25, 2021 7:42 am |
|
|
Hi All,
Temtronic: Thanks for pointing out the error in my OP!
Ttelmah: That is perfect! Exactly the idea I was looking for!
Thanks,
Jack |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19534
|
|
Posted: Fri Jun 25, 2021 8:02 am |
|
|
I've actually suggested to CCS, that they might want to consider adding the flags. There are two. + for leading plus, and - for left justify in the output
field. They are C features that at time can be useful.
I knew exactly what you were saying, since I had found this myself some
time ago.... |
|
|
JAM2014
Joined: 24 Apr 2014 Posts: 138
|
|
Posted: Fri Jun 25, 2021 11:26 am |
|
|
Hi All,
As a quick follow-up, a CCS fix would be nice, but in the meantime....
I was thinking that a more universal fix might be desirable that could accommodate different width variables. At the moment, I have both -10 to +10, and -125 to +125, so I currently need at least two formats. I have passed a 'count' variable to the new function, and handle things differently depending on a count of '2' or a count of '3', but it would be nice to have something more flexible. I haven't found a way to make the format specifier, eg. "+%02d", use a variable. How could that be done neatly/cleanly?
Thanks,
Jack |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1353
|
|
Posted: Fri Jun 25, 2021 1:16 pm |
|
|
JAM2014 just make sure not to use that function in other code without ensuring that the values are between -99 and +99 or it will overwrite memory. Once the values hit 3 digits your string variable inside the function needs to be at least a size 5 (currently a size 4) to hold the 1 digit sign, a 3 digit number, and the null character.
You could adjust it to be a 5 byte string and then you can use it universally. |
|
|
|