Conversions


Martin McBride, 2017-02-12
Tags none
Categories none

Sometimes you need to convert from one type of data to another. This is called type conversion or sometimes casting.

As an example, suppose we have the following to variables:

STRING s1 = "10"
STRING s2 = "12"

What happens if we add them, like this:

STRING s3 = s1 + s2
PRINT(s3)

The result may (or may not) surprise you - you get "1012". That is because the values are strings, and when you apply the + operator to 2 strings, it joins the strings together. It doesn't matter whether the strings represent number, like "10", or names like "Monday", the program treats them all the same, and just joins them together.

If you want to add the two strings as if they were numbers, you must first convert them to numbers, using a conversion function. Different languages have different names for the conversion functions, but in this example we will call the function toInteger. The function toInteger takes a string value as its input, and returns the equivalent integer value:

INT i1 = toInteger(s1)
INT i2 = toInteger(s2)
INT i3 = i1 + i1
PRINT(i3)

The first line takes the string s1 (which equals "10"), and converts it to an integer value 10, which it stores in variable i1. The next line does the same thing for s2.

Now when we add the i1 and i2, we are dealing with integer values not strings, so the result is 22.

Conversion versus casting

For GCSE, you can generally use the words conversion and casting interchangeably. They mean more or less the same thing - to take a value and change its type. Your exam board might have a preference for one or the other, ask you teacher and use whichever term they suggest.

However, in most languages, casting and conversion are slightly different things.

In the example above, we took a string value "10" and used the function toInteger to create an integer value 10. Using a function to create a new value from an existing value of a different type is called converting. You wouldn't normally call this casting (although some people do).

Casting is usually used to refer to certain type conversions which are built in to the language itself. For example in C like languages:

float x = 2.7;
int i = (int)x;

You can't assign a float value to an integer variable. Instead, you need to cast the float value to an integer value first. Notice the syntax - it isn't quite like a function call, the brackets surround the type you are converting to.

Casting is sometimes done automatically for example:

float x = 2.7 + 3;

In this case, we can't add 2.7 and 3, because they are different types - a float and an int. The value 3 must be converted to a float (3.0) before the addition can take place. This is done automatically. It is called an implicit cast. You can do the cast explicitly if you like:

float x = 2.7 + (float)3;

But this isn't necessary, as the float casting will be done whether you include (float) or not.

This is just a FYI. Use whichever term your teacher or exam board recommend.

Copyright (c) Axlesoft Ltd 2020