"Sputnik" help  
Sputnik Help
Language Reference - Datatypes - Strings (As Numbers)

Strings (As Numbers)

Make sure to take a look at the use of ++ operator on a string by clicking here since it explains how the ++ operator alters a string.

Unlike most other languages Sputnik supports the use of *= -= <<= and all the other operators on strings.

However unlike a numeric value each letter of the string is treated as an individual byte.

Since Sputnik strings are Unicode (UTF8) when you do an operation like this it may set the characters value above which is well out of range of a normal byte however when you try pack the string as ASCII or use it as ASCII it will cast the character to a byte anyway solving the problem entirely.

The operation such as | or ^ etc will be performed on each character in sequence.

The string may be cut down to size if the given operation requires it such as &.

In most languages strings simply become a numeric 0 when you try do math on them (or it may be a numeric value if the string contained a float or decimal) but here the string acts in its own unique way.

This will only trigger if both variables you are trying to do the operation on are strings so to avoid this behavior just simply make sure at least one of your variables is not a string.

This is not to be confused with the actual binary datatype.

Like Perl the & | ^ on strings works exactly the same as it does in Perl.

However unlike Perl every other operator also applies to the strings such as + - * / etc this may or may not be useful but it is there if you want it.

// We can use \xHEXCODE in the strings to define the bytes directly
$a = "\x65\x64";
$b = "\x65";
$a += $b;
say $a;
// PRINTS
// Òd

// Or just put the chars directly
$a = "ed";
$b = "e";
$a += $b;
say $a;
// PRINTS
// Òd

As stated above all the operators work like this and instead of returning a numeric value it will re-add the bytes to the string in their modified state.

It will try to never add more bytes than needed so if the operation made an Int32 you will have a string with 4 bytes as a result.

A potential problem with this system is some expressions might not produce expected results for example:

$a = "100";
$b = "200"; 
$a += $b; 
say $a; // prints: c``

This is because it's accepting the strings as binary there is a simple way to solve this problem you can cast the $b as an int or float for example:

$a = "100";
$b = "200";
$a += (float)$b;
say $a; // prints: 300

or

$a = "100";
$b = "200";
$a += (int)$b;
say $a; // prints: 300

Although (int) is a 32-bit integer and (float) is a 32-bit floating point number if you require higher numerical values it's recommend you use (Int64) instead of (int) and (double) instead of (float).

Go check out using Character literals as numbers since it falls within this spectrum.


Contact
Cheryl (uberfox@hotmail.com)
Homepage
http://ubersoft.org