January 1, 2005

WinTask - sprintf

'
' sprintf - return a formatted string
'

' Author: Joe Strazzere

'

 

'Formatting Specifications

'The first character of the format argument is always a percent sign (%).
'The last character of format is a letter code that determines the type of formatting.
'One or more format modifiers can appear between the first and last character of the format argument (see below).
'The possible letter codes are as follows:

' c  Prints a character from its decimal ASCII code.
' d  Prints the decimal integer portion of a number.
' e  Converts input to scientific notation.
' f  Pads with zeros to the right of the decimal point.
' g  Prints a decimal value while suppressing non-significant zeros.
' o  Prints the octal value of the integer portion of a number.
' s  Prints an unmodified string.
' x  Prints the hexadecimal value of the integer portion of a number.
'
' % Prints a literal percent sign (%).

'Modifying Formats

'The output generated by a particular formatting code can be modified.
'Three types of modifiers can appear between the percent sign (%) and the format code character:

' -  (justification) A hyphen (-) indicates that the printed output is to be left-justified in its field.
' field width   A number by itself or to the left of a decimal point,
'       indicates how many characters the field should be padded.
'       When this number is preceded by a 0, the padding is done with zeros
'      to the left of the printed value.
' precision    A number to the right of a decimal point indicates the maximum width
'       of the printed string or how many digits are printed to the right
'      of the output decimal point.

 

dim ptr as unsigned
ptr=allocate(255)

 

'ret=External("user32.dll", "wsprintfA", ptr, "[%255s]", "")
'ret=External("user32.dll", "wsprintfA", ptr, "[%x]", 45)

ret=External("user32.dll", "wsprintfA", ptr, "[%12d]", 45)

 

msgbox("Length of resulting string is "+str$(ret))

a$=peekstring$(ptr)
msgbox(a$)