Combining Data Types in a Variable

Modern high-level languages such as Visual Basic, C, or JavaScript are designed to take the drudgery out of mundane programming chores. Both Visual Basic and JavaScript support (at least as an option) weak data typing, which basically means that the language lets you write programs without requiring that you manage the data types stored in variables. In Visual Basic, for example, you can just throw all data into a “object” data type, which accepts anything. Visual Basic takes care of managing the underlying memory requirements for the data you provide.

The typical robot control language is designed to be small and fast because it's meant to be run on a small single-board computer or an even smaller microcontroller. The Visual Basic programming environment takes up megabytes of hard disk space; the average robot control program is under 1 kilobyte in size.

Because of their simplicity, programming languages for robot control often require strong data typing, where you—the programmer—do all the data-typing work yourself. The C language supports strong data typing.

It's not as hard as it seems. But if you're used to a language such as Visual Basic or JavaScript, learning the requirements of strong data typing may require a period of adjustment. One of the most difficult aspects of strong data typing is that you cannot directly mix two data types together in a single variable. The reason? The result of the mixing probably won't fit in the memory space allocated for the variable. If you add a byte and a string together in an integer variable, the memory will hold only the one byte.

When you try to mix data types, either the programming environment you're using will display an error or the robot will not function correctly. In fact, it could function erratically, possibly damaging itself, so exercise care! Here's an example of data types that cannot be mixed when used in a programming environment that requires strong data typing:

Dim X as Byte
Dim Y as Word
Dim Z as Byte
X = 12
Y = 1025
Z = X + Y

Both X and Z are byte-length variables, each of which holds 8-bit values (that is, 0 to 255). Y is a word-length variable, which can hold a 16-bit value (e.g., 0 to 65535). The statement Z – X + Y will fail because Z cannot hold the contents of Y. If the programming environment doesn't catch this error, it'll create a bug in your robot.

At best, Z will hold the value 13 and not 1037, as you'd otherwise expect. The value 13 is what's left of 1037 when that number is stored in a space of only 8 bits.

Just because strong data typing restricts you from mixing and matching different data types doesn't mean you can't do it. The trick is to use the data conversion statements provided by the programming language. The most common data conversion is between integer numbers and text. Assuming the programming language you're using supports them, you might use something like Val to convert a string to a numeric type and Str to convert a numeric into a string:

Dim X as String
Dim Y As Integer
X = "A"
Y = Val (X)    ' Y contains the value 65;
               ' the ASCII equivalent of "A"

You must also exercise care when using numeric types of different sizes. Data conversion statements are typically provided in strong data typing programming languages to convert 8-, 16-, 32-, and (sometimes) 64-bit numbers from one form to another.

If the programming language supports floating-point numbers (numbers that have digits to the right of the decimal point), then there will likely be data conversion statements for those as well.