Core Function SPrintf

From Sputnik Wiki
Jump to: navigation, search
SPrintf( <format control>, <params> )

Contents

Description

Returns a formatted string (similar to the C sprintf() function).

Parameters

format control

The format and flags to use (see Remarks).

This can be either a string or an array that will be converted into a string

params

Variables that will be output according to the "format control".

The variables can also be arrays with their own unique designated separator.

When using arrays you may have any/all/none of the following Keys (Case-Sensitive):

KEY              WHAT IT DOES
@Sep             Allows you to define a separator for each element in the array to use
@Begin           Allows you to define a string to add to the beginning of each element in the array
@End             Allows you to define a string to add to the end of each element in the array

See example for more information.

If there is just one param and it is a full dictionary using keys/values only then it will treated very differently see example for dictionary stuff.

Return Value

Success: Return the formatted string according to "variable format" defined in the "format control" parameter.

Failure: Returns "" (blank string).

Remarks

As with all Sputnik functions Sprintf() is fully Unicode so you don't need to worry about such things.

If you wish to immediately print to the console rather than return a string you should see Printf( <format control>, <params> ).

Unlike AutoIt (And similar programs) there is no buffer overflow chance, each "variable" is not limited to any amount characters it can be as big as it needs to be (Because Sputnik handles the entire sprintf parsing/operation/printing etc internally and does not use DLLs or APIs to accomplish it.

If you want to have "%" in the format control you must enter "%%".

"variable format" is; %[flags] [width] [.precision] type

       Width Specification

The second optional field of the format specification is the width specification. The width argument is a nonnegative decimal integer controlling the minimum number of characters printed. If the number of characters in the output value is less than the specified width, blanks are added to the left or the right of the values — depending on whether the – flag (for left alignment) is specified — until the minimum width is reached. If width is prefixed with 0, zeros are added until the minimum width is reached (not useful for left-aligned numbers).

The width specification never causes a value to be truncated. If the number of characters in the output value is greater than the specified width, or if width is not given, all characters of the value are printed (subject to the precision specification).

       Type Specification
Type        Variable type           Output format 
b           Integer/Float           Binary number (Detects if it should use Integer or Float based on value type). 
B           Long Integer/Float      Binary number (Detects if it should use Integer or Float based on value type). 
d, i        Integer                 Signed decimal integer. 
D           Long Integer            Signed decimal long integer. 
o           Integer                 Unsigned octal integer. 
O           Long Integer            Unsigned octal long integer. 
u           Integer                 Unsigned decimal integer. 
U           Long Integer            Unsigned decimal long integer. 
x           Integer                 Unsigned hexadecimal integer, using "abcdef". 
X           Integer                 Unsigned hexadecimal integer, using "ABCDEF". 
a           Float                   Hexadecimal floating point in lowercase, using "abcdef". 
A           Float                   Hexadecimal floating point in uppercase, using "ABCDEF". 
e           Float                   Signed value having the form [ - ]d.dddd e [sign]ddd where d is a single decimal
                                    digit, dddd is one or more decimal digits, ddd is exactly three decimal digits,
                                    and sign is + or -. 
E           Float                   Identical to the e format except that E rather than e introduces the exponent. 
f           Float                   Signed value having the form [ - ]dddd.dddd, where dddd is one or more decimal digits.
                                    The number of digits before the decimal point depends on the magnitude of the number,
                                    and the number of digits after the decimal point depends on the requested precision. 
g           Float                   Signed value printed in f or e format, whichever is more compact for the given value
                                    and precision. The e format is used only when the exponent of the value is less
                                    than -4 or greater than or equal to the precision argument. Trailing zeros are
                                    truncated, and the decimal point appears only if one or more digits follow it. 
G           Float                   Identical to the g format, except that E, rather than e, introduces the exponent
                                    (where appropriate). 
p           Pointer address         Prints the pointer address in hex with a 0x prefix.
P           Pointer address         Prints the pointer address in hex (UPPER CASE) with a 0x prefix.
s           String                  String. 
S           String                  String (Will force Binary/Arrays etc to print as String). 
c           Character               A single character (ASCII).
C           Character               A single character (UNICODE).
V           SV                      Interpret integer as Sputnik's standard integer type.
n           Nothing printed         The corresponding argument must be a variable to be used.
                                    The number of characters written so far will stored in the variable provided.
       Flag Specification
Flag       Meaning                                                            Default 
^          Format parameter index                                             No explicit index
           An explicit format parameter index, such as 2^.
           By default Sprintf() will format the next unused argument
           in the list, but this allows you to take the arguments out
           of order and even use the same argument multiple times.
           See the examples below.
'          Custom padding will replace the usual space/zero with              No padding.
           a custom padding of your choosing. You must enter the
           custom padding character immediately after the ' for
           example %'# will cause # to become the custom character.
           This padding by default will left and right sides of the
           input as the width specifies. 
`          Custom left align padding will replace the usual space/zero        No padding.
           that appear when you do a left alignment (or center) the
           custom character will appear visible on the right side
           after the item. Using a custom padding of your choosing.
           This is useful if you wish to use the usual custom padding
           on top of this padding to allow you to choose unique characters
           to surround both the left and right side of your input.           
           You must enter the custom padding character immediately after
           the ` for example %`# will cause # to become the custom character
!          Center align the result within the given field width.              Center align.
-          Left align the result within the given field width.                Right align. 
+          Prefix the output value with a sign (+ or -) if
           the output value is of a signed type.                              Sign appears only for negative signed
                                                                              values (-). 
0          If width is prefixed with 0, zeros are added until the
           minimum width is reached. If 0 and - appear, the 0 is
           ignored. If 0 is specified with an integer format
           (i, u, x, X, o, d) the 0 is ignored.                               No padding. 
Blank      Prefix the output value with a blank if the output value
           is signed and positive; the blank is ignored if both the
           blank and + flags appear.                                          No blank appears. 
#          When used with the o, x, or X format, the # flag prefixes
           any nonzero output value with 0, 0x, or 0X, respectively.          No blank appears. 
#          When used with the e, E, or f format, the # flag forces
           the output value to contain a decimal point in all cases.          Decimal point appears only if digits follow it. 
#          When used with the g or G format, the # flag forces the
           output value to contain a decimal point in all cases
           and prevents the truncation of trailing zeros.
           Ignored when used with c, d, i, u, or s.                           Decimal point appears only if digits follow it.
                                                                              Trailing zeros are truncated. 
       Width Specification
width             description
(number)	  Minimum number of characters to be printed. If the
                  value to be printed is shorter than this number,
                  the result is padded with blank spaces. The value
                  is not truncated even if the result is larger.
*	          The width is not specified in the format string, but
                  as an additional integer value argument preceding
                  the argument that has to be formatted.
       Precision Specification

The third optional field of the format specification is the precision specification. It specifies a nonnegative decimal integer, preceded by a period (.), which specifies the number of characters to be printed, the number of decimal places, or the number of significant digits (see Table below). Unlike the width specification, the precision specification can cause either truncation of the output value or rounding of a floating-point value. If precision is specified as 0 and the value to be converted is 0, the result is no characters output, as shown below:

sprintf( "%.0d", 0 ); /* No characters return */

        Specifiers

The length sub-specifier modifies the length of the data type. This is a chart showing the types used to interpret the corresponding arguments with and without length specifier (if a different type is used, the proper type promotion or conversion is performed, if allowed):

length          d i          u o x X          f F e E g G a A          c          s          p          n
(none)	        Int32	     UInt32           Double	               Int32      String     IntPtr     Ref
hh	        SByte        Byte                                                                       Ref
h	        Int16        UInt16                                                                     Ref
l	        Int32        UInt32           Float                    Char	  String                Ref
ll              Int64        UInt64                                                                     Ref
j	        Int64        UInt64                                                                     Ref
z	        Int64        UInt64                                                                     Ref
t	        Int64        UInt64                                                                     Ref
L                                             Double                                                    Ref

Some of the above are obsolete and may be removed later (hh, h, l, ll will stay forever though)

When you place a variable into the parameter and say the variable is a max size Int64 if you enter %hhd the variable size will be cut down to the the a byte you can use this as a form of casting instead of having to explicitly cast.

The "l" can be used to cast a show a double as if it was a float such as %ld similar to casting.

   How Precision Values Affect Type
Type                       Meaning                                         Default  
d, i, u, o, x, X           The precision specifies the minimum
                           number of digits to be printed. If the
                           number of digits in the argument is less
                           than precision, the output value is padded
                           on the left with zeros. The value is not
                           truncated when the number of digits exceeds
                           precision.                                      Default precision is 1.  
e, E                       The precision specifies the number of digits
                           to be printed after the decimal point.
                           The last printed digit is rounded.              Default precision is 6; if precision is 0
                                                                           or the period (.) appears without a number
                                                                           following it, no decimal point is printed.  
f                          The precision value specifies the number of
                           digits after the decimal point. If a decimal
                           point appears, at least one digit appears
                           before it. The value is rounded to the
                           appropriate number of digits.                   Default precision is 6; if precision is 0,
                                                                           or if the period (.) appears without a number
                                                                           following it, no decimal point is printed.  
g, G                      The precision specifies the maximum number of
                          significant digits printed.                      Six significant digits are printed, with any
                                                                           trailing zeros truncated.  
s                         The precision specifies the maximum number of
                          characters to be printed. Characters in excess
                          of precision are not printed.                    Characters are printed until a null character
                                                                           is encountered.  

Example

Before we begin

// Anything you can do in Printf you can return it as a string using Sprintf
// Like so..
$value = sprintf("%%e = '%e'\n", 777);
print($value); // Prints: 7.770000e+002

Format parameter index in this example we explicitly tell it to use parameter 2 where you would expect 1 to go and 1 where you would expect 2 to go

$value = sprintf('%2^d %1^d', 12, 32);
print($value);
// Prints
// 32 12

Format parameter index in this example we explicitly use the same parameter in multiple formats

$value = sprintf("As Int '%1^d' As Hex '%1^#x' As Char '%1^c'\n", 12);
print($value);
// Prints
// As Int '12' As Hex '0xc' As Char '♀'

Format parameter index of course everything still works such as padding

$value = sprintf("As Int '%1^6d' As Hex '%1^#-6x' As Char '%1^!6c'\n", 12);
print($value);
// Prints
// As Int '    12' As Hex '0xc   ' As Char '  ♀   '

A basic example

$d = 5;
$m = 11;
$y = 1990;
say sprintf("%02d/%02d/%04d", $d, $m, $y);
// 05/11/1990

Example of using custom padding

$s = 'monkey';
$t = 'many monkeys';
 
printf("[%s]\n",      $s); // standard string output
printf("[%10s]\n",    $s); // right-justification with spaces
printf("[%-10s]\n",   $s); // left-justification with spaces
printf("[%010s]\n",   $s); // zero-padding works on strings too
printf("[%'#10s]\n",  $s); // use the custom padding character '#'
printf("[%'#-10s]\n",   $s); // left-justification with custom padding '#'
printf("[%10.10s]\n", $t); // left-justification but with a cutoff of 10 characters
printf("[%'#10d]\n",  777); // use the custom padding character '#'
printf("[%'#10g]\n",  777.42); // use the custom padding character '#'
printf("[%'#-10d]\n",  777); // left-justification with custom padding '#'
printf("[%'#-10g]\n",  777.42); // left-justification with custom padding '#'
/*
[monkey]
[    monkey]
[monkey    ]
[0000monkey]
[####monkey]
[monkey####]
[many monke]
[#######777]
[####777.42]
[777#######]
[777.42####]
*/

Example of center alignment

// Normal spaces
printf("[%!20d]\n", 1);
printf("[%!20d]\n", 10);
printf("[%!20d]\n", 100);
printf("[%!20d]\n", 1000);
 
printf("[%!20g]\n", 1.1);
printf("[%!20g]\n", 10.2);
printf("[%!20g]\n", 100.3);
printf("[%!20g]\n", 1000.4);
 
printf("[%!20s]\n", "The");
printf("[%!20s]\n", "Quick");
printf("[%!20s]\n", "Brown");
printf("[%!20s]\n", "Fox");
 
// Custom padding
printf("[%'-!20d]\n", 1);
printf("[%'-!20d]\n", 10);
printf("[%'-!20d]\n", 100);
printf("[%'-!20d]\n", 1000);
 
printf("[%'-!20g]\n", 1.1);
printf("[%'-!20g]\n", 10.2);
printf("[%'-!20g]\n", 100.3);
printf("[%'-!20g]\n", 1000.4);
 
printf("[%'-!20s]\n", "The");
printf("[%'-!20s]\n", "Quick");
printf("[%'-!20s]\n", "Brown");
printf("[%'-!20s]\n", "Fox");
 
// Custom padding on left and right (different chars for each)
printf("[%'>`<!20d]\n", 1);
printf("[%'>`<!20d]\n", 10);
printf("[%'>`<!20d]\n", 100);
printf("[%'>`<!20d]\n", 1000);
 
printf("[%'>`<!20g]\n", 1.1);
printf("[%'>`<!20g]\n", 10.2);
printf("[%'>`<!20g]\n", 100.3);
printf("[%'>`<!20g]\n", 1000.4);
 
printf("[%'>`<!20s]\n", "The");
printf("[%'>`<!20s]\n", "Quick");
printf("[%'>`<!20s]\n", "Brown");
printf("[%'>`<!20s]\n", "Fox");
/*
[         1          ]
[         10         ]
[        100         ]
[        1000        ]
[      1.10000       ]
[      10.2000       ]
[      100.300       ]
[      1000.40       ]
[        The         ]
[       Quick        ]
[       Brown        ]
[        Fox         ]
[---------1----------]
[---------10---------]
[--------100---------]
[--------1000--------]
[------1.10000-------]
[------10.2000-------]
[------100.300-------]
[------1000.40-------]
[--------The---------]
[-------Quick--------]
[-------Brown--------]
[--------Fox---------]
[>>>>>>>>>1<<<<<<<<<<]
[>>>>>>>>>10<<<<<<<<<]
[>>>>>>>>100<<<<<<<<<]
[>>>>>>>>1000<<<<<<<<]
[>>>>>>1.10000<<<<<<<]
[>>>>>>10.2000<<<<<<<]
[>>>>>>100.300<<<<<<<]
[>>>>>>1000.40<<<<<<<]
[>>>>>>>>The<<<<<<<<<]
[>>>>>>>Quick<<<<<<<<]
[>>>>>>>Brown<<<<<<<<]
[>>>>>>>>Fox<<<<<<<<<]
*/

Example of using a variable to choose padding size

$t = 'f';
printf("right-justified variable width (padded-zeros): '%0*c'\n", 10, $t);
printf("left-justified variable width (padded-zeros): '%-'0*c'\n", 10, $t);
printf("right-justified variable width (padded-custom): '%'#*c'\n", 10, $t);
printf("left-justified variable width (padded-custom): '%-'#*c'\n", 10, $t);
// right-justified variable width: '         f'
// left-justified variable width : 'f         '
// right-justified variable width (padded-zeros): '000000000f'
// left-justified variable width (padded-zeros): 'f000000000'
// right-justified variable width (padded-custom): '#########f'
// left-justified variable width (padded-custom): 'f#########'

Example of printing integers and floats in hexadecimal

say "Hex of 32-bit Integer";
say sprintf('%x', @INT32_MAX);
say sprintf('%x', @INT32_MIN);
 
say "Hex of 64-bit Integer";
say sprintf('%llx', @INT64_MAX);
say sprintf('%llx', @INT64_MIN);
 
say "Hex of 32-bit Floating Point";
say sprintf('%a', 777.42);
say sprintf('%a', -777.42);
 
say "Hex of 64-bit Floating Point";
say sprintf('%la', 777.42);
say sprintf('%la', -777.42);
/*
Hex of 32-bit Integer
7fffffff
80000000
Hex of 64-bit Integer
7fffffffffffffff
8000000000000000
Hex of 32-bit Floating Point
44425ae1
c4425ae1
Hex of 64-bit Floating Point
40884b5c28f5c28f
c0884b5c28f5c28f
*/

Example %n

$text = "World !";
Printf("Hello%n %s\n%n", $val, $text, $val2);
Printf("%%n at first check was %d\n", $val);
Printf("%%n at second check was %d\n", $val2);
// Prints
// Hello World !
// %n at first check was 5
// %n at second check was 14

Example %p to print a memory address etc

$ptr = Alloc(500);
Printf("Pointer: %p\n", $ptr); // Pointer: 0x06d5bda8
Printf("Pointer: %P\n", $ptr); // Pointer: 0x06D5BDA8
free($ptr);

Large example

$n = 43951789;
$u = -43951789;
 
// notice the double %%, this prints a literal '%' character
Printf("%%d = '%d'\n", $n);             //'43951789'          standard integer representation
Printf("%%e = '%e'\n", $n);             //'4.395179e+07'     scientific notation
Printf("%%u = '%u'\n", $n);             //'43951789'          unsigned integer representation of a positive integer
Printf("%%u <0 = '%u'\n", $u);          //'4251015507'        unsigned integer representation of a negative integer
Printf("%%f = '%f'\n", $n);             //'43951789.000000'   floating point representation
Printf("%%.2f = '%.2f'\n", $n);         //'43951789.00'       floating point representation 2 digits after the decimal point
Printf("%%o = '%o'\n", $n);             //'247523255'         octal representation
Printf("%%s = '%s'\n", $n);             //'43951789'          string representation
Printf("%%x = '%x'\n", $n);             //'29ea6ad'           hexadecimal representation (lower-case)
Printf("%%X = '%X'\n", $n);             //'29EA6AD'           hexadecimal representation (upper-case)
 
Printf("%%+d = '%+d'\n", $n);           //'+43951789'         sign specifier on a positive integer
Printf("%%+d <0= '%+d'\n", $u);         //'-43951789'         sign specifier on a negative integer
 
 
$s = 'monkey';
$t = 'many monkeys';
 
Printf("%%s = [%s]\n", $s);             //[monkey]            standard string output
Printf("%%10s = [%10s]\n", $s);         //[    monkey]        right-justification with spaces
Printf("%%-10s = [%-10s]\n", $s);       //[monkey    ]        left-justification with spaces
Printf("%%010s = [%010s]\n", $s);       //[0000monkey]        zero-padding works on strings too
Printf("%%10.10s = [%10.10s]\n", $t);   //[many monke]        left-justification but with a cutoff of 10 characters
 
Printf("%04d-%02d-%02d\n", 2008, 4, 1);

If you are wondering how the hell to do a 64-bit signed integer

// Recommended way
Printf("Int64 value is: '%D' ok\n", @Int64_Max);
// Alternative
Printf("Int64 value is: '%lld' ok\n", @Int64_Max);

If you are wondering how the hell to do a 64-bit unsigned integer

// Recommended way
Printf("UInt64 value is: '%U' ok\n", @UInt64_Max);
// Alternative
Printf("UInt64 value is: '%llu' ok\n", @UInt64_Max);

Example of using arrays

$n = 777;
$t = array(100, 200, 300, 400);
Printf("Single value is '%d' but lets do an array of values '%d' cool huh?\n", $n, $t);

Example of using arrays with separator

$n = 777;
$t = array(@Sep => "-", 100, 200, 300, 400);
Printf("Single value is '%d' but lets do an array of values '%d' cool huh?\n", $n, $t);

Example of using arrays with separator but this time dont include the separator in the array

$n = 777;
$t = array(100, 200, 300, 400);
Printf("Array of values '%d'\n", [@Sep => "/"] . $t);
// Original array is intact
printr $t; // see
// Prints
// Array of values '100/200/300/400'
// ARRAY
// {
//         [0] => 100
//         [1] => 200
//         [2] => 300
//         [3] => 400
// }

Large example with arrays

$ints = array(@Sep => "-", 100, 200, 300, 400);
$floats = array(@Sep => "#", 100.2, 777.42, 133.77, 88.1);
$strings = array(@Sep => "#", "One", "Two", "Three");
Printf("Ints '%d' \nFloats '%g' \nStrings '%s'\n", $ints, $floats, $strings);

Example using @Sep, @Begin and @End

$arr = array(@Sep => "\n", @Begin => "0x", @End => "<-- Hex yup", 0..50);
Printf("Hex of the array\n%x\n", $arr);

More examples

Printf(  "%+d\n", 42   ); # "+42"
Printf(  "%4d\n", 42   ); # "  42"
Printf(  "%04d\n", 42   ); # "0042"
Printf(  "%X\n", 0x1337f00d   ); # "1337F00D"

Using an array as the format string

$arr = array("huey", "dewey", "louie");
say sprintf($arr); # 'huey dewey louie'
 
$arr = array(10, 11, 12);
say sprintf('%x', $arr); # 'abc'
 
$arr = array(@Sep => " ", 10, 11, 12);
say sprintf('%x', $arr); # 'a b c'
 
$arr = array(@Sep => " ", 65, 66, 67);
say sprintf('%c', $arr); # 'A B C'
 
$arr = array(@Sep => '; ', 1, 2, 3);
say sprintf('%02d', $arr); # '01; 02; 03'

Using an array as the format string with custom join

$arr = Join(array("huey", "dewey", "louie"),'-');
say sprintf($arr); # 'huey-dewey-louie'

Using an array (as a dictionary) as the format string

$arr = array("foo" => 1, "bar" => 2);
say sprintf($arr); # 'foo     1
                   #  bar     2'
 
$arr = array("foo" => 1, "bar" => 2, "testingFooBar" => 3);
say sprintf($arr); # 'foo               1
               #  bar               2
               #  testingFooBar     3'

Using an array (as a dictionary) as the format string with custom join

$arr = JoinKV(array("foo" => 1, "bar" => 2),"<BR>\n");
say sprintf($arr); # 'foo     1<BR>
                   #  bar     2'

Using an array (as a dictionary) as the only param

$arr = array("Apples" => 5, "Oranges" => 10);
say sprintf('%s cost %d euros', $arr);
             # 'Apples cost 5 euros
             #  Oranges cost 10 euros'
 
// Of course you can still use the @Sep
$arr = array(@Sep => ";", "Apples" => 5, "Oranges" => 10);
say sprintf('%s cost %d euros', $arr);
             # 'Apples cost 5 euros;Oranges cost 10 euros'
 
// Of course @Begin and @End are useable too
$arr = array(@Begin => ">>>", @End => "<<<", "Apples" => 5, "Oranges" => 10);
say sprintf('%s cost %d euros', $arr);
             # '>>>Apples cost 5 euros<<<
             #  >>>Oranges cost 10 euros<<<'
 
// Can use them all
$arr = array(@Sep => ";", @Begin => ">>>", @End => "<<<", "Apples" => 5, "Oranges" => 10);
say sprintf('%s cost %d euros', $arr);
             # '>>>Apples cost 5 euros<<<;>>>Oranges cost 10 euros<<<'
 
$arr = array(@Sep => " -- ", "huey" => 1, "dewey" => 2, "louie" => 3);
say sprintf('%s', $arr);
             # 'huey -- dewey -- louie'
 
$arr = array(@Sep => " -- ", "huey" => 1, "dewey" => 2, "louie" => 3);
say sprintf('%s(%d)', $arr);
             # 'huey(1) -- dewey(2) -- louie(3)'
 
say sprintf("%s is %.3f AU away from the sun", "Earth", 1);
             # Prints "Earth is 1.000 AU away from the sun"
 
say sprintf("%s is %.3f AU away from the sun", array("Earth" => 1));
             # Prints "Earth is 1.000 AU away from the sun"

Yet another example

$n =  43951789;
$u = -43951789;
$c = 65; // ASCII 65 is 'A'
 
// notice the double %%, this prints a literal '%' character
Printf("%%b = '%b'\n", $n); // binary representation (32 bit size)
Printf("%%B = '%B'\n", @Int64_Max); // binary representation (64 bit size)
Printf("%%c = '%c'\n", $c); // print the ascii character, same as chr() function
Printf("%%d = '%d'\n", $n); // standard integer representation
Printf("%%e = '%e'\n", $n); // scientific notation
Printf("%%u = '%u'\n", $n); // unsigned integer representation of a positive integer
Printf("%%u = '%u'\n", $u); // unsigned integer representation of a negative integer
Printf("%%f = '%f'\n", $n); // floating point representation
Printf("%%o = '%o'\n", $n); // octal representation
Printf("%%s = '%s'\n", $n); // string representation
Printf("%%x = '%x'\n", $n); // hexadecimal representation (lower-case)
Printf("%%X = '%X'\n", $n); // hexadecimal representation (upper-case)
 
Printf("%%+d = '%+d'\n", $n); // sign specifier on a positive integer
Printf("%%+d = '%+d'\n", $u); // sign specifier on a negative integer

Example of using %b to print the number in its binary form

Printf("Binary of Int32 value is: '%b'\n", @Int32_Max);
Printf("Binary of Int64 value is: '%B'\n", @Int64_Max);
Printf("Binary of Float value is: '%b'\n", @Float_Max);
Printf("Binary of Double value is: '%B'\n", @Double_Max);

// Printing a binary $variable

$binary = BinaryFromStr("Hello World!");
Printf("Binary variable to Hex = '%X'\n", $binary);
Printf("Binary variable to Hex (with spaces) = '%3X'\n", $binary);
Printf("Binary variable to Chars = '%c'\n", $binary);
Printf("Binary variable to Numbers = '%d'\n", $binary);
Printf("Binary variable to String = '%S'\n", $binary);
// Prints
// Binary variable to Hex = '48656C6C6F20576F726C6421'
// Binary variable to Hex (with spaces) = ' 48 65 6C 6C 6F 20 57 6F 72 6C 64 21'
// Binary variable to Chars = 'Hello World!'
// Binary variable to Numbers = '72101108108111328711111410810033'
// Binary variable to String = 'Hello World!'

Example of using %V

say "Now singles";
Printf("Value is: '%V' ok\n", (char)65);
Printf("Value is: '%V' ok\n", null);
Printf("Value is: '%V' ok\n", true);
Printf("Value is: '%V' ok\n", false);
Printf("Value is: '%V' ok\n", @SBYTE_MAX);
Printf("Value is: '%V' ok\n", @BYTE_MAX);
Printf("Value is: '%V' ok\n", @INT16_MAX);
Printf("Value is: '%V' ok\n", @INT32_MAX);
Printf("Value is: '%V' ok\n", @INT64_MAX);
Printf("Value is: '%V' ok\n", @UINT16_MAX);
Printf("Value is: '%V' ok\n", @UINT32_MAX);
Printf("Value is: '%V' ok\n", @UINT64_MAX);
Printf("Value is: '%V' ok\n", @FLOAT_MAX);
Printf("Value is: '%V' ok\n", @DOUBLE_MAX);
Printf("Value is: '%V' ok\n", "Hello");
 
say "Now arrays";
Printf("Value is: '%V' ok\n", array((char)65, (char)'a'));
Printf("Value is: '%V' ok\n", array(null, null, null));
Printf("Value is: '%V' ok\n", array(true, false, (bool)1, (bool)0));
Printf("Value is: '%V' ok\n", array((sbyte)77, (sbyte)11, (sbyte)22));
Printf("Value is: '%V' ok\n", array((byte)100, (byte)200, (byte)202));
Printf("Value is: '%V' ok\n", array((int16)777, (int16)778, (int16)778));
Printf("Value is: '%V' ok\n", array((int32)1337, (int32)7331, (int32)1373));
Printf("Value is: '%V' ok\n", array((int64)131, (int64)121, (int64)141));
Printf("Value is: '%V' ok\n", array((uint16)1, (uint16)2, (uint16)3));
Printf("Value is: '%V' ok\n", array((uint32)111, (uint32)222, (uint32)333));
Printf("Value is: '%V' ok\n", array((uint64)1110, (uint64)2220, (uint64)3330));
Printf("Value is: '%V' ok\n", array((float)777.77, (float)777.11, (float)777.22));
Printf("Value is: '%V' ok\n", array((float)42.77, (float)42.11, (float)42.22));
Printf("Value is: '%V' ok\n", array("Hello", "World", "ok"));
 
say "Now arrays with separator";
Printf("Value is: '%V' ok\n", array(@Sep => ", ", (char)65, (char)'a'));
Printf("Value is: '%V' ok\n", array(@Sep => ", ", null, null, null));
Printf("Value is: '%V' ok\n", array(@Sep => ", ", true, false, (bool)1, (bool)0));
Printf("Value is: '%V' ok\n", array(@Sep => ", ", (sbyte)77, (sbyte)11, (sbyte)22));
Printf("Value is: '%V' ok\n", array(@Sep => ", ", (byte)100, (byte)200, (byte)202));
Printf("Value is: '%V' ok\n", array(@Sep => ", ", (int16)777, (int16)778, (int16)778));
Printf("Value is: '%V' ok\n", array(@Sep => ", ", (int32)1337, (int32)7331, (int32)1373));
Printf("Value is: '%V' ok\n", array(@Sep => ", ", (int64)131, (int64)121, (int64)141));
Printf("Value is: '%V' ok\n", array(@Sep => ", ", (uint16)1, (uint16)2, (uint16)3));
Printf("Value is: '%V' ok\n", array(@Sep => ", ", (uint32)111, (uint32)222, (uint32)333));
Printf("Value is: '%V' ok\n", array(@Sep => ", ", (uint64)1110, (uint64)2220, (uint64)3330));
Printf("Value is: '%V' ok\n", array(@Sep => ", ", (float)777.77, (float)777.11, (float)777.22));
Printf("Value is: '%V' ok\n", array(@Sep => ", ", (float)42.77, (float)42.11, (float)42.22));
Printf("Value is: '%V' ok\n", array(@Sep => ", ", "Hello", "World", "ok"));
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox