Keysight VISA.NET Help
Formatting Integers

Formatting Integers

An integer argument has a format specifier in one of the following forms:

%d%[-| |+|@[1|2|3|H|Q|B]][[0]width][.precision][,[arraySize]]d

%i%[-| |+|@[1|2|3|H|Q|B]][[0]width][.precision][,[arraySize]]i

%u%[-| |+|@[1|2|3|H|Q|B]][[0]width][.precision][,[arraySize]]u

%o%[-| |+|@[ Q|B]][[0]width][.precision][,[arraySize]]o

%x%[-| |+|@[H| B]][[0]width][.precision][,[arraySize]]x

%X%[-| |+|@[H| B]][[0]width][.precision][,[arraySize]]X

Modifier

Interpretation

Default Functionality

The argument is interpreted as an integer.

flags

-, +, space, 0

@1, @2, @3

@H, @Q, @B

Controls justification and padding of the output, as follows:

  • '-' left-aligns the result with the given field width.
  • '+' prefixes the value with the sign (- or +).   Ignored with @H, @Q, or @B.
  • ' ' prefixes the value with the sign (- or space).   Ignored with @H, @Q, or @B.
  • '0' pads the value with '0'.   Ignored with '-', @H, @Q, or @B.
  • '@1' formats the value in IEEE 488.2 NR1 format.
  • '@2' formats the value in IEEE 488.2 NR2 format.
  • '@3' formats the value in IEEE 488.2 NR3 format.
  • '@H' formats the value in IEEE 488.2 hexadecimal format.
  • '@Q' formats the value in IEEE 488.2 octal format.
  • '@B' formats the value in IEEE 488.2 binary format.

width

Minimum field width of the output string.

An asterisk (*) may be present in lieu of an integer width modifier, in which case an extra Int32 argument supplies the value.

.precision

Maximum number of characters to send.

An asterisk (*) may be present in lieu of an integer precision modifier, in which case an extra Int32 argument supplies the value.

[,[arraySize]]

Format an array of integers converted to strings.  If arraySize is specified, it is the maximum number of elements to include in the formatted array.

An asterisk (*) may be present in lieu of an integer arraySize modifier, in which case an extra Int32 argument supplies the value.

Integer Formatting Code Snippets

Remember that Printf is adding characters to the formatted write buffer. The comments after each Printf call show what character(s) are added to the formatted write buffer by that call. Assume that the io variable is a valid reference to IMessageBasedFormattedIO.

No Modifiers
Copy Code
io.Printf("|%d|", 15);           // Adds |15|
io.Printf("|%i|", 15);           // Adds |15|
io.Printf("|%o|", 15);           // Adds |17|  (octal)
io.Printf("|%u|", 15);           // Adds |15|
io.Printf("|%x|", 15);           // Adds |f|  (hexadecimal, lowercase)
io.Printf("|%X|", 15);           // Adds |F|  (hexadecimal, uppercase)
io.Printf("|%d|\n", -15);        // Adds |-15|, sends buffer & flushes
Sign Modifiers
Copy Code
io.Printf("|% d|", 15);          // Adds | 15|
io.Printf("|% d|", -15);         // Adds |-15|
io.Printf("|%+d|", 15);          // Adds |+15|
IEEE 488.2 Format Modifiers
Copy Code
io.Printf("|%@1d|", 15);         // Adds |15|
io.Printf("|%@2d|", 15);         // Adds |15.000000|
io.Printf("|%@38d|", 15);        // Adds |1.500000E+001|
io.Printf("|%-@B8d|", 15);       // Adds |#B1111  |
io.Printf("|%@Q8o|", 15);        // Adds |    #Q17| (octal)
io.Printf("|%-@H8x|", 15);       // Adds |#HF     | (hexadecimal, lowercase)
io.Printf("|%@H8X|", 15);        // Adds |     #HF| (hexadecimal, uppercase)
Width and Precision Modifiers
Copy Code
io.Printf("|%8d|", 15);          // Adds |      15|
io.Printf("|%-*d|", 8, 15);      // Adds |15      |
io.Printf("|%.08d|", 15);        // Adds |00000015|
io.Printf("|%.0*d|", 8, 15);     // Adds |00000015|
PrintfArray
Copy Code
unsafe
{
    Int32[] data = new Int32[] { 1, 1, 1 };
    fixed (Int32* pdata = data)
    {
        io.PrintfArray("|%,2d|", pdata);   // Adds |1,1|
    }
}    

 

 


© Keysight Technologies 2015-2025