I/O Format Specifiers for Writing Integers
Integer Data — %d (and variants)
Section titled “Integer Data — %d (and variants)”An integer argument has a format specifier of the following form:
%[flags] [488.2 type] [width] [.precision] [delimiter [array size] ] [$S] [b | h | l | I] [d | o | x]
| Modifier | Interpretation |
|---|---|
| Default Functionality | The argument is formatted as an integer in NR1 format (an integer without a decimal point). |
flags +, -, 0 | Controls justification and padding of the output, as follows: - - left aligns the result with the given field width.+ - prefixes the output value with a sign (+ or –) if the output value is of a signed type. By default, a sign only appears for negative values.0 pads with zeros until the specified width is reached. If 0 and - appear together, the 0 is ignored. |
488.2 type | The number is formatted in one of six standard IEEE 488.2 numeric formats. The supported formats are: @1 - IEEE 488.2 NR1 format (integer without any decimal point). Example: 123@2 - IEEE 488.2 NR2 format (number with at least one digit after the decimal point). Example: 123.45@3 - IEEE 488.2 NR3 format (floating-point number in exponential form). Example: 1.2345E-67@H - IEEE 488.2 hex format. Example: #HAF35B@Q - IEEE 488.2 octal format. Example: #Q71234@B - IEEE 488.2 binary format. Example: #B011101001 |
width | Minimum field width of the output number. An asterisk (*) may be present in lieu of an integer width modifier, in which case an extra int argument supplies the value. |
precision | Minimum number of digits to send. An asterisk (*) may be present in lieu of an integer precision modifier, in which case an extra int argument supplies the value. |
delimiter [array size] | The argument is interpreted as an array of integers (or long or short integers, as specified by the length). If the $S modifier is present, the argument is interpreted as a SAFEARRAY of integers. array size specifies the number of array elements to send. An asterisk (*) may be present in lieu of an integer array size modifier, in which case an extra int argument supplies the value. |
$S | The argument is interpreted as a SAFEARRAY of integers. |
length modifier | If no length modifier is specified, then the argument is assumed to be a long integer (32-bits). b - argument is a BYTE (8-bit) integer.h - argument is a short (16-bit) integer.l - argument is a long (32-bit) integer.I - argument is a __int64 (64-bit) integer. |
type | d - number is formatted as a decimal integer.o - number is formatted as an octal integer.x - number is formatted as a hex integer. |
Example — Field width control
Section titled “Example — Field width control”HRESULT hr = S_OK;
int nCount = 1234;
// Left justifyhr = io.Printf(_T("%-8d"), nCount); // Sends "1234 "
// Right justifyhr = io.Printf(_T("%8d"), nCount); // Sends " 1234"
// Right justify, pad with zeroeshr = io.Printf(_T("%08d"), nCount); // Sends "00001234"
// Supply field width using a variableint nWidth = 5;hr = io.Printf(_T("%0*d"), nWidth, lCount); // Sends "00123";Example — Integer length modifiers
Section titled “Example — Integer length modifiers”HRESULT hr = S_OK;
int nCount = 1234;hr = io.Printf(_T("%d"), nCount); // Sends "1234"
long lCount = 1234;hr = io.Printf(_T("%ld"), lCount); // Sends "1234"
short sCount = 1234;hr = io.Printf(_T("%hd"), sCount); // Sends "1234"Example — Integer arrays
Section titled “Example — Integer arrays”HRESULT hr = S_OK;
int rgData[] = { 1, 2, 3, 4, 5 };
// Comma-separated list of 3 elementshr = io.Printf(_T("%,3d"), rgData); // Sends "1,2,3"
// Semi-colon separated, count supplied as a variableint nCount = 4;hr = io.Printf(_T("%(;)*d"), nCount, rgData); // Sends "1;2;3;4"
// Field width specified applies to each elementhr = io.Printf(_T("%4,3d"), rgData); // Sends " 1, 2, 3"
CComSafeArray<int> saData;saData.Add(1);saData.Add(3);saData.Add(3);
// Comma-separated list. Argument is a SAFEARRAY.// No field width supplied, so count is determined from the SAFEARRAY itself.hr = io.Printf(_T("%,$Sd"), saData.m_psa); // Sends "1,2,3"Example — IEEE 488.2 format numbers
Section titled “Example — IEEE 488.2 format numbers”HRESULT hr = S_OK;
// Format value as an IEEE-defined hex numberint nMask = 1234;hr = io.Printf(_T("%@Hd"), nMask); // Sends "#H7B"
// Format value as an IEEE-defined binary numberhr = io.Printf(_T("%@Hd"), nMask); // Sends "#B1111011"
int rgData[] = { 1, 2, 3, 4, 5 };
// Field width of 5 includes the "#B", pad with zeroshr = io.Printf(_T("%@B05,5d"), rgData); // Sends "#B001,#B010,#B011,#B100,#B101"