Format Methods put whatever output they produce into the csOutput object. The simplest example is:
FORMAT
METHOD Constant__
/* just outputs the string passed into it */
CODE
csOutput = csParam1;
ENDCODE
PARAM "String to print" Str
Because custom Format Methods are the most popular, we will give another example. This example formats a string of bytes as unsigned decimal integers. It is equivalent in function to the standard method called StringOfDecimal, but is coded differently.
FORMAT
METHOD MyStringOfDecimal___
/* formats a string of decimal bytes */
CODE
int iSize = isizbitCurrentField / 8;
int iStart = iofsbitCurrentField / 8;
int iPrintSize = min(iParam1, iSize);
char* pOutput = csOutput.GetBuffer(iPrintSize*4+5);
pOutput[0] = '\0';
for (int i = 0; i < iPrintSize; i++)
sprintf(&pOutput[i*4], "%-3u ", abytLayer[iStart+i]);
csOutput.ReleaseBuffer();
if (iPrintSize != iSize)
csOutput += "...";
ENDCODE
PARAM "Length of display string" int
Since this method must be capable of handling a field that is longer than 8 bytes (64 bits) we cannot use i64CurrentField. Instead we access the field data through the byte array called abytLayer which holds the undecoded data for the protocol layer. The value in iofsbytCurrentPosition gives us the index into this array of the first byte of the current field. We simply loop through the bytes formatting each one with spaces in between. A lot of the code simply deals with making sure the string ends up being of appropriate length and adding an ellipsis if there is any truncation. The one parameter is the maximum length of the formatted string in characters.
If a method that works directly with abytLayer is called for a field that happens to be 64 bits or less in size then the effect of any Retrieval Methods applied to the field value will not be reflected in the value that is formatted.