Casting

Casting is the term given to how a value can be converted to a different data type.

All data is stored as binary, but different data is stored in different ways, for example:

  • The character ‘7’ is stored as 00110111
  • The number 7 is stored as a BYTE as 0000111
  • The number 7 is stored as a floating point (REAL) number as 01000000111000000000000000000000


Some languages require that you state the datatype when you declare your variables (e.g. VB.net and C# ).

Other languages automatically change the datatype depending on the type of data that is assigned to a variable (e.g. Python and Javascript).

The most common place you will see casting taking place is when a user enters data. INPUT commands return data as a string, so if you ask a user to enter a numeric value you will need to convert it from a string to a numeric data type.

Example Casting Functions:

  • Int(data) converts the data to an integer
  • Real(data) converts the data to a real number
  • Str(number) converts a number to a string
  • Chr(number) converts a number to its equivalent ASCII character
  • Asc(character) converts a character to its equivalent ASCII number