It can also be handy to format your data to be displayed, or convert the data representation. Here is an example, with max 999 character output, and 4 (optional) parameters with their AHK types.
sprintf(format, f0="",v0="",f1="",v1="",f2="",v2="",f3="",v3="") { ; f = Double, Int64(->%I64..), Int, Str, Char VarSetCapacity(S,999) DllCall("msvcrt\sprintf", Str,S, Str,format, f0,v0, f1,v1, f2,v2, f3,v3) Return S }It can be used for converting numbers to hex integers
HEX(n) { VarSetCapacity(S,20) Return DllCall("msvcrt\sprintf", Str,S, Str,"0x%I64x", Int64,n) ? S : S } MsgBox % Hex(1025) MsgBox % Hex(-1)sprintf does not handle single precision float parameters. Accordingly, real number format types (e, f, g) process 64-bit (double) arguments, so always specify "Double" for the f0, f1… parameters (never "float").
Integer types (d, i, o, u, x) take 32-bit integer parameters, so for f0, f1… specify "Int" or "UInt". If you want 64-bit integer types, in the format parameter precede the types with I64 (I64d, I64i, I64o, I64u or I64x) specify "Int64" for the f0, f1… parameters.
String type (s) is also supported with f = "Str", like char © with f = "Char", and char counting (n). Here is a summary of the format placeholder: %[flags][width][.precision][length]type
flags can be omitted or be any of:
'+' : Causes sprintf to always denote the sign '+' or '-' of a number (the default is to omit the sign for positive numbers). Only applicable to numeric types.
'-' : Causes sprintf to left-align the output of this placeholder (the default is to right-align the output).
'#' : Alternate form. For 'g' and 'G', trailing zeros are not removed. For 'f', 'F', 'e', 'E', 'g', 'G', the output always contains a decimal point. For 'o', 'x', and 'X', a 0, 0x, and 0X, respectively, is prepended to non-zero numbers.
' ' : (space) Causes sprintf to left-pad the output with spaces until the required length of output is attained. If combined with '0' (see below), it will cause the sign to become a space when positive, but the remaining characters will be zero-padded
'0' : Causes sprintf use '0' (instead of spaces) to left fill a fixed length field. For example (assume i = 3) sprintf("%2d", i) results in " 3", while printf("%02d", i) results in "03"
Width can be omitted or be any of:
a number : Causes sprintf to pad the output of this placeholder with spaces until it is at least number characters wide. If number has a leading '0', then padding is done with '0' characters.
'*' : Causes sprintf to pad the output until it is n characters wide, where n is an integer value stored in the a function argument just preceding that represented by the modified type.
.Precision can be omitted or be any of:
a number : For non-integral numeric types, causes the decimal portion of the output to be expressed in at least number digits. For the string type, causes the output to be truncated at number characters.
'*' : Same as the above, but uses an integer value in the intaken argument to determine the number of decimal places or maximum string length. For example, printf("%.*s", 3, "abcdef") will result in "abc" being printed.
If the precision is zero, nothing is printed for the corresponding argument.
Length can be omitted or be:
'I64' : 64-bit integer type
type can be any of:
'd', 'i' : Print an int as a signed decimal number.
'u' : Print decimal unsigned int.
'f', 'F' : Print a double in normal (fixed-point) notation.
'e', 'E' : Print a double value in standard form ([-]d.ddd e[+/-]ddd).
'g', 'G' : Print a double in either normal or exponential notation, whichever is more appropriate for its magnitude. 'g' uses lower-case letters, 'G' uses upper-case letters. Insignificant zeroes to the right of the decimal point are not included and the decimal point is not included on whole numbers.
'x', 'X' : Print an unsigned int as a hexadecimal number. 'x' uses lower-case letters and 'X' uses upper-case.
'o' : Print an unsigned int in octal.
's' : Print a character string.
'c' : Print a char (character).
'p' : Print a pointer in 8 digit hex format with leading 0's.
'n' : Write number of characters successfully written so far into an integer pointer parameter.
'%' : Print a literal '%' character (this type doesn't accept any flags, width, precision or length).
You can test them with something like the following
VarSetCapacity(p,20) MsgBox % sprintf("%p%n","Int",65, "Int",&p) MsgBox % "Previously " . *&p | *(&p+1)<<8 . " chars were written" MsgBox % sprintf("%s / %s = %c","Str","Numerator","Str","Denominator","Char",65) MsgBox % sprintf("%12.9e, %f","Double",-400*atan(1),"Double",12345678901234567.0) MsgBox % sprintf("%d, %d, %I64d","Int",-2,"Int",9876543210987654,"Int64",9876543210987654) ; v2 overflows