| Using VISA.NET > Formatted I/O > Using Scanf > Scanf Code Snippets > Reading Integers |
An integer argument has a format specifier in one of the following forms:
d[*|@[1|H|Q|B]][width][,[arraySize]]d
i[*|@[1|H|Q|B]][width [,[arraySize]]i
u[*|@[1|H|Q|B]][width][,[arraySize]]u
o[*|@[Q|B]][width][,[arraySize]]o
x[*|@[H|B]][width][,[arraySize]]x
X[*|@[H|B]][width][,[arraySize]]X
|
Modifier |
Interpretation |
|---|---|
|
Default Functionality |
The data in the read buffer is parsed as an integer or array of integers. |
|
flags * @1 @H, @Q, @B |
Controls parsing, as follows:
|
|
width |
The maximum number of characters to be parsed for this specifier. A hash character (#) may be present in lieu of an integer width modifier, in which case the value is specified in the Scanf inputs array argument. |
|
,[arraySize] |
Parse a comma-separated array of integers. If arraySize is specified, it is the maximum number of elements to include in the formatted array. A hash character (#) may be present in lieu of an integer arraySize modifier, in which case the value is specified in the Scanf inputs array argument. |
Remember that Scanf is reading bytes from the formatted read buffer, parsing them according to the format specifiers, and placing the results in an output variable. The comment preceding each line shows the data being parsed by the snippet and the resulting value that is returned in the corresponding output argument, like this: // "<data to parse>", "<value>". The <value> may optionally be followed by a comment in parentheses.
Assume that the io variable is a valid reference to IMessageBasedFormattedIO. Assume that resultn is an out argument declared as a .NET signed integer. Any size integer type is capable of holding any of the integers used in these snippets.
| No Modifiers |
Copy Code |
|---|---|
// "8, -16, 32\n", 8, -16, 32
io.Scanf("%d, %i, %u\n", out result1, result2, result3); // "17, f, 1A\n", 15, 15, 26 io.Scanf("%o, %x, %X\n", out result1, result2, result3); | |
| Flags |
Copy Code |
|---|---|
// "13, 17, f, 1A\n", 15, 15, 26 io.Scanf("%*o, %o, %x, %X\n", out result1, result2, result3); | |
| IEEE 488.2 Format Modifiers |
Copy Code |
|---|---|
// "123\n", 123 io.Scanf("@1d*t", out result1); // "#B110110\n", 54 io.Scanf("@Bd*t", out result1); // "#Q14\n", 12 io.Scanf("@Qd*t", out result1); // "#H1A\n", 26 io.Scanf("@HX*t", out result1); | |
| Width Modifier |
Copy Code |
|---|---|
// "123456\n", 12 io.Scanf("2d*T", out result1); // "123456\n", 12 io.Scanf("#d*T", new Int64[] {2}, out result1); | |
The following two snippets demonstrate a known issue with width and the IEEE @H, @Q, and @B flags. Note that in the first snippet, Scanf correctly uses the width of 4 to read and parse "0X1A" from the response. In the second snippet, Scanf does not count the initial "#H" against the width, and so a width of 2 is used to parse "#H1A" so that 26 is returned.
| Known Issue |
Copy Code |
|---|---|
// "0X1AFFF\n", 26 io.Scanf("4X*T", out result1); // "#H1AFFF\n", 26 io.Scanf("@H2X*T", out result1); | |
| ScanfArray |
Copy Code |
|---|---|
// "1, 2, 3, 4\n", { 1, 2, 3, 4 } - Memory must be allocated by the calling program. Int32[10] out1; io.ScanfArray("%,d%*T", out1); | |