<< Click to Display Table of Contents >> Navigation: 2. Components > 3. System-Variable and Specialfolders > 3.5 Hexadezimal, Decimal, Binary and more |
MiniRobotLanguage (MRL)
Hexadecimal & numeric formats
Intention
In normal life, we most often use only "decimal" numbers. In some situation it is easier to specify a number in "Hexadecimal" notation.For example when specifying a color-value.
Also as you may know, computers only work with binary data. BInary means it has only the two values 0 and 1 - it is base 2. Hexadecimal has all numbers you can see below, it is base 16.
It is easier to convert Binary to Hex and vice-verse compared to standard numbers (base 10). By grouping Binary numbers by 4, you can actually convert it directly to Hex, and vice versa. Here are all HEX-numbers and their binary conversion.
0 = 0000
1 = 0001
2 = 0010
3 = 0011
4 = 0100
5 = 0101
6 = 0110
7 = 0111
8 = 1000
9 = 1001
A = 1010
B = 1011
C = 1100
D = 1101
E = 1110
F = 1111
For example:
In short B0D3 (using the above table) converts to binary as 1011 0000 1101 0011
So if you write something like 1101001010010011, and group it by four 1101 0010 1001 0011. Then use the table and you get "D293".
The robot will understand Hexadezimal numbers if they are prefixed with either "&H0" (unsigned numbers). Or "&H" (signed numbers).
With base 10, you will need to compute it manually (for A0D3 see below)
A = 10 * 16^3 (or 10 * 16 * 16 * 16) = 40960
0 = 0 * 16^2 (or 0 * 16 * 16) = 0
D = 13 * 16^1 (or 13 * 16) = 208
3 = 3 * 16^0 (or 3 * 1) = 3
then just add the results and get the decimal number, in this case 41171.
You can use the CCV.-command to directly get the values of any System-Color.
Here is an example:
' Show value od System color S2
CCV.H|S2|$$HEX
DBP.$$HEX
ENR.
The CCV.-Command can also convert numbers into Hexadecimal Color values:
' Show value od System color S2
$$NUM=12345368
CCV.H|$$NUM|$$HEX
DBP.$$HEX
ENR.
Details
You can use these numeric formats at many places in your scripts:
1. Decimal
You can just write a number, if the number has a fractional part, use the "." for example:
CAL.$$RES=4.5*9
Decimal numbers can be prefixed with a "-" sign to make them have a negative value.
2. Octal
You can use Octal numbers, if you prefix the number with "&O". Octal numbers will be converted to signed decimal values. For example:
CAL.$$RES=&10*9
' Result is 72 (dec)
Octal numbers are signed numbers and may not contain an fractional part.
3. Hexa-Decimal
You can use signed or unsigned Hexadecimal numbers. They may not have an fractional part. Be sure to choose the right Prefix! If you specify
&H(number) -> signed number conversion
&H0(number) -> unsigned number conversion
For example:
CAL.$$RES=&HFFFF*9
DBP.$$RES
ENR.
' Result is -9
CAL.$$RES=&H0FFFF*9
DBP.$$RES
ENR.
' Result is 589815
To convert a decimal number to a Hexadecimal number, use
VTH. - Variable to Hexadecimal
4. Binary
You can specify binary values as well. Prefix them with "&B". No fractional part can be used.
For example:
VAN.$NUM=&B01010011
DBP.$$NUM
' Result is 83
CAL.$$RES=&B10*9
DBP.$$RES
' Result is 18
To convert a decimal number to a BInary number, use
Numeric parameters and formulas
Wherever you have numeric (decimal-) parameters, you can also use a formula.
For example:
The command PAU. want a numeric parameter first. We could write:
' The script will Pause for 5 seconds
PAU.5
We can also use a variable here:
' The script will Pause for 5 seconds
: §§TIM=5
PAU.§§TIM
Now we can also use a formula here. The trick is, that we must put the formula into brackets ( ...).
' The script will Pause for 5 seconds
: §§TIM=1
PAU.(§§TIM*5)
You can use formulas at most places where numeric parameters are valid. The formula can use all expressions that are valid for the CAL.-command also.
Remarks
Prefer using decimal numbers in formulas to prevent confusion. If you place the numbers inside variables, using the VAN. Command they are converted immediately to decimal numbers, which is the best way to go.
If you use the VAR. command instead, the numbers will be stored as text inside the variable.
Limitations:
-
See also:
• VTH. - Variable to Hexadecimal
• CAL. - mathematical CALculation
• CCD. - Calculate Color Distance
See further:
•3.5 Hexadezimal, Decimal and Binary and more
•3.6 Using Quadrant-Coordinates