sprintf — formats and stores sets of symbols and values..

 

Description:

u8 sprintf(u8 *buf, u8 *string[, u8 arg-list…]);

Saves the arguments from arg-list list into buf under the control of string string and returns the length of converted string buf. Every argument arg-list (if any) is converted and displayed in accordance with corresponding format specification specified in string.

List of parameters:

buf

Pointer to the string.

string

String or pointer to the format control string. Format control string consists of normal characters, escape-sequences and if the arguments follow the format string then it includes format specification. If the arguments arg-list follow the format string then this string is also have to contain the format specifications determining the outputs of these arguments. Format specification always starts from percent symbol (%).
Format string is read left-to-right. When the format specification (if any) occurs the value of the first argument after the format string is converted and is displayed in accordance with specified specification. The second format specification causes the conversion and displaying of the second argument and so on so forth till the end of the format string. If arguments more than format specifications then the additional arguments are ignored.
Format specification takes the following form:

%[flags][wigth][.precision]type.

Where:

% – format specification symbol;

[flags] – Turning on of displaying and printing the characters, spaces, octal and hexadecimal prefixes (Table flags).
[wigth] – Minimum number of displayed characters (Description of wigth is below).

[.precision] – Maximum number of characters (for %s) or minimum number of digits (for printing the integer values %d %u %i).

type – format specification (Table type).

Table of format specification (type)

Symbol Description:
s String of symbols.
p Pointer value
d Signed decimal number of integer type
i Signed decimal number of integer type
e Scientific notation (е of lowercase)
E Scientific notation (E of uppercase)
o Unsigned octal integer
u Unsigned decimal number of integer type
x Unsigned hexadecimal integer (letters of lowercase)
X Unsigned hexadecimal integer (letters of uppercase)
% Symbol
%
c Symbols

Values flags

Flag Value Default value
Result outdent within the field wigth. right
+ Symbol appending to the displayed value if it has character type.. The symbol “-” appears only for negative character values.
# When using with o, x, X formats the flag # appends 0, 0х, 0Х correspondently to any nonzero displayed value . Without prefix.

Width – non-negative decimal integer that controls the number of printed characters. If the number of characters in the output is less than in width, then the spaces are added on the left and right (depending on where the flag “-” is specified) unless the minimum width is reached. If 0 is appended to width then 0 will be added till the minimum width is reached. (It is not applicable to the outdent numbers).
The star (*) may occur in specification width when instead of value the argument corresponding to it is put from the list of arguments . Argument width is to precede the corresponding value.

arg-list

Variable number of parameters.

Return values:

The function returns buf string length after conversion.
Examples:

<--! Example 1 -->
<item addr="524:248" name="Test for sprintf function"; type="script">
u8 count = 0;
 
V-ID/V-ADDR {
        	if(opt0()){
                    	// string for result output to the interface
                    	u8 strForMess[100];                    	
                    	u8 str[] = "hello";                    	
                    	if(count == 0) {
                                // %s  String of symbols
                                u8 resultOfFunc = sprintf(strForMess, "%cString - %s and %s, len = ", 1, str, "world");
                              	ltoa(strForMess+strlen(strForMess), resultOfFunc);
                               	setStatus(@exciterId():32, &strForMess);                               	
                               	setStatus(V-ADDR, 0);
                               	++count;
                               	return;
                    	}
                    	if(count == 1) {
                               	// %p - pointer value
                                sprintf(strForMess, "%cAddresses - %p %p %p %p", 4, str, str + 1, str + 2, str + 3);
                               	setStatus(@exciterId():32, &strForMess);                               	
                               	setStatus(V-ADDR, 0);
                               	++count;
                               	return;
                    	}
                    	if(count == 2) {
                               	// %d - Signed decimal number of integer type
                               	u8 value_1 = 55;
                               	i8 value_2 = -55;
                                sprintf(strForMess, "%c%d %d %d %d", 8, 1, -2, value_1, value_2);
                               	setStatus(@exciterId():32, &strForMess);                               	
                               	setStatus(V-ADDR, 0);
                               	++count;
                               	return;
                    	}
                    	if(count == 3) {
                               	// %i  - Signed decimal number of integer type
                               	u8 value_1 = 22;
                               	i8 value_2 = -33;
                               	sprintf(strForMess, "%c%i %i %i %i", 1, 1, -2, value_1, value_2);
                               	setStatus(@exciterId():32, &strForMess);                               	
                               	setStatus(V-ADDR, 0);
                               	++count;
                               	return;
                    	}
                    	if(count == 4) {
                               	// %е   Scientific notation  (е of lowercase)
                               	sprintf(strForMess, "%c%e", 4, 3000, 2000);
                               	setStatus(@exciterId():32, &strForMess);                               	
                               	setStatus(V-ADDR, 0);
                               	++count;
                               	return;
                    	}
                    	if(count == 5) {
                               	// %е   Scientific notation  (E of uppercase)
                               	sprintf(strForMess, "%c%E", 8, 3000, 2000);
                               	setStatus(@exciterId():32, &strForMess);                               	
                               	setStatus(V-ADDR, 0);
                               	++count;
                               	return;
                    	}
                    	if(count == 6) {
                               	// %о   Unsigned octal integer
                               	sprintf(strForMess, "%cOctal system: %o", 1, 10);
                               	setStatus(@exciterId():32, &strForMess);                               	
                               	setStatus(V-ADDR, 0);
                               	++count;
                               	return;
                    	}
                    	if(count == 7) {
                               	// %u  Signed decimal number of integer type
                               	i8 value_1 = 10;
                               	i8 value_2 = -10;
                               	sprintf(strForMess, "%c%u %u", 4, value_1, value_2);
                               	setStatus(@exciterId():32, &strForMess);                               	
                               	setStatus(V-ADDR, 0);
                               	++count;
                               	return;
                    	}
                    	if(count == 8) {
                                // %х   Unsigned hexadecimal integer (letters of lowercase)
                                sprintf(strForMess, "%cHexadecimal number systemа lov: 0x%x", 8, 255);
                               	setStatus(@exciterId():32, &strForMess);                               	
                               	setStatus(V-ADDR, 0);
                               	++count;
                               	return;
                    	}
                    	if(count == 9) {
                                // %Х  Unsigned hexadecimal integer (letters of uppercase)
 		                sprintf(strForMess, "%cHexadecimal number system hi: 0x%X, 1, 255);
                               	setStatus(@exciterId():32, &strForMess);                               	
                               	setStatus(V-ADDR, 0);
                               	++count;
                               	return;
                    	}
                    	if(count == 10) {
                               	// %% Displays symbol %
                               	sprintf(strForMess, "%c%%", 4);
                               	setStatus(@exciterId():32, &strForMess);                           	
                               	setStatus(V-ADDR, 0);
                               	++count;
                               	return;
                    	}
                        if(count == 11) {
                               	// %c   Displays symbols
                               	u8 val_1 = 0x41; // A
                               	u8 val_2 = 0x40; // @
                               	u8 val_3 = 0x55; // U
                               	u8 *val_4 = "+- %#";
                                sprintf(strForMess, "%cSymbols: %c %c %c %c %c %c", 8, val_1, val_2, val_3, val_4[4], 0x67);
                               	setStatus(@exciterId():32, &strForMess);                               	
                               	setStatus(V-ADDR, 0);
                               	count = 0;
                               	return;
                    	}
        	}
}
</item>
 
 
<--! Example 2 -->
<item addr="524:248" name="Test for sprintf function" type="script">
u8 count = 0;
 
V-ID/V-ADDR {
        	if(opt0()) {
                    	// string for result output to the interface
                    	u8 strForMess[200];                	
                    	if(count == 0) {
                               	// usage of width
                               	u8 *str = "Hello World";
                                sprintf(strForMess, "%cwidth with a string (with and without -): _%20s_ _%-20s_", 1, str, str);
                               	setStatus(@exciterId():32, &strForMess);                             	
                               	setStatus(V-ADDR, 0);
                               	++count;
                               	return;
                    	}
                    	if(count == 1) {
                               	// usage of width
                                sprintf(strForMess, "%cwidth with a number (with and without -): _%20d_ _%-20d_", 4, 33, 33);
                               	setStatus(@exciterId():32, &strForMess);                               	
                               	setStatus(V-ADDR, 0);
                               	++count;
                               	return;
                    	}
                    	if(count == 2) {
                               	// usage of width
                                sprintf(strForMess, "%cwidth with a number (*with and without -): _%*d_ _%-*d_", 8, 7, 33, 7, 33);
                               	setStatus(@exciterId():32, &strForMess);                               	
                               	setStatus(V-ADDR, 0);
                               	++count;
                               	return;
                    	}
                    	if(count == 3) {
                               	// flag#
                                sprintf(strForMess, "%cOctal(%%o) and hexadecimal(%%x, %%X) without and with the flag #: \10%%o - %o, %#o\10%%x - %x, %#x\10%%X - %X,  %#X", 1, 200, 200, 200, 200, 200, 200);
                               	setStatus(@exciterId():32, &strForMess);                               	
                               	setStatus(V-ADDR, 0);
                               	++count;
                               	return;
                    	}
                    	if(count == 4) {
                               	// precision
                               	u8 *str = "Hello world";
                                sprintf(strForMess, "%c.precision with a string  %s\10%%s - %s\10%%.4s - %.4s", 4, str, str, str);
                               	setStatus(@exciterId():32, &strForMess);                             	
                               	setStatus(V-ADDR, 0);
                               	++count;
                               	return;
                    	}
                    	if(count == 5) {
                               	// precision
                                sprintf(strForMess, "%c.precision with a number %d\10%%d - %d\10%%.6d - %.6d", 8, 43, 43, 43);
                               	setStatus(@exciterId():32, &strForMess);                               	
                               	setStatus(V-ADDR, 0);
                               	++count;
                               	return;
                    	}
                    	if(count == 6) {
                               	// Flag#
                               	i8 val_1 = -33;
                               	i8 val_2 = 55;                            	
                                sprintf(strForMess, "%cFlag +\10Number -33 with flag %+d, and withoutз %d.\10Number 55 with flag %+d, and without %d.", 1, val_1, val_1, val_2, val_2);
                               	setStatus(@exciterId():32, &amp;strForMess);                           	
                               	setStatus(V-ADDR, 0);
                               	count = 0;
                               	return;
                    	}
        	}
}
</item>

The result of performing the examples in the interface::

The result of example 1 when the element script is clicked sequentially
“String – Hello World, len = 38″
“Addresses – current value of pointers“
“1 -2 55 -55″
“1 -2 22 -33”
“9.881313e-321”
“9.881313E-321”
“Octal system: 12”
“10 4294967286”
“Hexadecimal system lov: 0xff”
“Hexadecimal system hi:: 0xFF”
“%”
“Symbols: A @ U # g”

The result of example 2 when the element script is clicked sequentially

“cwidth with a string (with and without -): _ Hello World_ _ Hello World _”
“width with a number (with and without -): _ 33_ _33 _”
“width with a number (with and without -): _ 33_ _33 _”

“ Octal(%o) and Hexadecimal(%x, %X) without and with the flag #:
%o – 310, 0310
%x – c8, 0xc8
%X – C8, 0XC8”

“.precision with the string Hello world
%s – Hello world
%.4s – Hell”

“.precision with number 43
%d – 43
%.6d – 000043”

“Flag+
Number -33 with flag -33,and without -33.
Number 55 with flag +55,and without 55”