Language > Data Types > Type Conversion Functions

# Type Conversion Functions

IDL allows you to convert data from one data type to another using a set of conversion functions. These functions are useful when you need to force the evaluation of an expression to a certain type, output data in a mode compatible with other programs, etc. For a list of type conversion functions, see Type Conversion. Conversion functions operate on data of any structure: scalars, vectors, or arrays, and variables can be of any type.

### Take Care When Converting Types

If the variable you are converting lies outside the range of the type to which you are converting, IDL will truncate the binary representation of the value without informing you. For example:

; Define A. Note that the value of A is outside the range

; of integers, and is automatically created as a longword

; integer by IDL.

A = 33000

;B is silently truncated.

B = FIX(A)

PRINT, B

IDL prints:

-32536

Applying FIX creates a short (16-bit) integer. If the value of the variable passed to FIX lies outside the range of 16-bit integers, IDL will silently truncate the binary value, returning only the 16 least-significant bits, with no indication that an error has occurred.

With most floating-point operations, error conditions can be monitored using the FINITE and CHECK_MATH functions.

### Converting Strings

When converting from a string argument, it is possible that the string does not contain a valid number and no conversion is possible. The default action in such cases is to print a warning message and return zero. The ON_IOERROR procedure can be used to establish a statement to be jumped to in case of such errors.

Conversion between strings and byte arrays (or vice versa) is something of a special case. The result of the BYTE function applied to a string or string array is a byte array containing the ASCII codes of the characters of the string. Converting a byte array with the STRING function yields a string array or scalar with one less dimension than the byte array.

### Dynamic Type Conversion

The TYPE keyword to the FIX function allows type conversion to an arbitrary type at runtime without the use of CASE or IF statements on each type. The following example demonstrates the use of the TYPE keyword:

PRO EXAMPLE_FIXTYPE

; Define a variable as a double:

A = 3D

; Store the type of A in a variable:

typeA = SIZE(A, /TYPE)

PRINT, 'A is type code', typeA

; Prompt the user for a numeric value:

READ, UserVal, PROMPT='Enter any Numeric Value: '

; Convert the user value to the type stored in typeA:

ConvUserVal = FIX(UserVal, TYPE=typeA)

PRINT, ConvUserVal

END

### Examples of Type Conversion

See the following table for examples of type conversions and their results.

Operation | Results
FLOAT(1) | 1.0
FIX(1.3 + 1.7) | 3
FIX(1.3) + FIX(1.7) | 2
FIX(1.3, TYPE=5) | 1.3000000
BYTE(1.2) | 1
BYTE(-1) | 255b (Bytes are modulo 256)
BYTE(’01ABC’) | [48b, 49b, 65b, 66b, 67b]
STRING([65B, 66B, 67B]) | ’ABC’
FLOAT(COMPLEX(1, 2)) | 1.0
COMPLEX([1, 2], [4, 5]) | [COMPLEX(1,4),COMPLEX(2,5)]