Sprintf Subroutine

Attached is a subroutine that mimics the sprintf/printf function of C. It may be useful for formatting output for PAC display or for packing data for character based protocols (like xml or json). It’s in PAC Control 9.3 basic.

If you’ve used the “c” printf function before, then usage should be familiar to you, if not ----well search sprintf man page and that should get you started. The arguments are passed in through a pointer table, so some familiarity with pointers is helpful.

Let me know if you run into any bugs - I’ve tested it a bit, but have not used it for anything in production yet.

Example usage:


//Populate pointer table
ptInputs[0] = &sData;
ptInputs[1] = &nLengthSoFar;
ptInputs[2] = &nLengthSoFar;
ptInputs[3] = &fData;
ptInputs[4] = &nData;
ptInputs[5] = &nIndex;
ptInputs[6] = &ntData;
ptInputs[7] = &PID_Temperature;


sData="Hello World!";
nData = 50;
fData = 3.141593;
nIndex=5;
ntData[nIndex] = 54321;


Specifier="String: %*5$.*5$s%n<-Position: %i Everyone likes %.3e! Integer: %i%% ntData[%i]: %znt*5$i Temperature: %#.1zaif °F";
sprintf(Specifier, ptInputs, sOutput1);


Specifier="<xmltag attrib='%s'>$%4$'.2f</xmltag>"; //Using positional argument %4$ (not zero based!)
sprintf(Specifier, ptInputs, sOutput2);


//sOutput1: String: Hello<-Position: 13 Everyone likes 3.142e+00! Integer: 50% ntData[5]: 54321 Temperature: 78.0 °F
//sOutput2: <xmltag attrib='Hello World!'>$3.14</xmltag>


sprintf 9.3Basic.zip (9.09 KB)