Next Previous Contents

13. Data-Type Conversion Functions

13.1 atof

Synopsis

Convert a string to a double precision number

Usage

Double_Type atof (String_Type s)

Description

This function converts a string s to a double precision value and returns the result. It performs no error checking on the format of the string. The function _slang_guess_type may be used to check the syntax of the string.

Example

     define error_checked_atof (s)
     {
        if (__is_datatype_numeric (_slang_guess_type (s)))
          return atof (s);
        throw InvalidParmError, "$s is not a double"$;
    }

See Also

typecast, double, _slang_guess_type

13.2 atoi

Synopsis

Convert a string to an integer

Usage

Int_Type atoi (String_Type str)

Description

The atoi function converts a string to an Int_Type using the standard C library function of the corresponding name.

Notes

This function performs no syntax checking upon its argument.

See Also

integer, atol, atoll, atof, sscanf

13.3 atol

Synopsis

Convert a string to an long integer

Usage

Long_Type atol (String_Type str)

Description

The atol function converts a string to a Long_Type using the standard C library function of the corresponding name.

Notes

This function performs no syntax checking upon its argument.

See Also

integer, atoi, atoll, atof, sscanf

13.4 atoll

Synopsis

Convert a string to a long long

Usage

LLong_Type atoll (String_Type str)

Description

The atoll function converts a string to a LLong_Type using the standard C library function of the corresponding name.

Notes

This function performs no syntax checking upon its argument. Not all platforms provide support for the long long data type.

See Also

integer, atoi, atol, atof, sscanf

13.5 char

Synopsis

Convert a character code to a string

Usage

String_Type char (Integer_Type c)

Description

The char function converts an integer character code (ascii) value c to a string of unit character length such that the first character of the string is c. For example, char('a') returns the string "a".

If UTF-8 mode is in effect (_slang_utf8_ok is non-zero), the resulting single character may be represented by several bytes.

If the character code c is less than 0, then byte-semantics will be used with the resulting string consisting of a single byte whose value is that of -c&0xFF.

Notes

A better name should have been chosen for this function.

See Also

integer, string, string<@@ref>typedeftypedef, sprintf, pack

13.6 define_case

Synopsis

Define upper-lower case conversion

Usage

define_case (Integer_Type ch_up, Integer_Type ch_low)

Description

This function defines an upper and lowercase relationship between two characters specified by the arguments. This relationship is used by routines which perform uppercase and lowercase conversions. The first integer ch_up is the ascii value of the uppercase character and the second parameter ch_low is the ascii value of its lowercase counterpart.

Notes

This function has no effect in UTF-8 mode.

See Also

strlow, strup

13.7 double

Synopsis

Convert an object to double precision

Usage

Double_Type double (x)

Description

The double function typecasts an object x to double precision. For example, if x is an array of integers, an array of double types will be returned. If an object cannot be converted to Double_Type, a type-mismatch error will result.

Notes

The double function is equivalent to the typecast operation

     typecast (x, Double_Type)
To convert a string to a double precision number, use the atof function.

See Also

typecast, atof, int

13.8 int

Synopsis

Typecast an object to an integer

Usage

Int_Type int (s)

Description

This function performs a typecast of an object s to an object of Integer_Type. If s is a string, it returns returns the ascii value of the first bytes of the string s. If s is Double_Type, int truncates the number to an integer and returns it.

Example

int can be used to convert single byte strings to integers. As an example, the intrinsic function isdigit may be defined as

    define isdigit (s)
    {
      if ((int (s) >= '0') and (int (s) <= '9')) return 1;
      return 0;
    }

Notes

This function is equivalent to typecast (s, Integer_Type);

See Also

typecast, double, integer, char, char<@@ref>isdigitisdigit, char<@@ref>isdigitisdigit<@@ref>isxdigitisxdigit

13.9 integer

Synopsis

Convert a string to an integer

Usage

Integer_Type integer (String_Type s)

Description

The integer function converts a string representation of an integer back to an integer. If the string does not form a valid integer, a SyntaxError will be thrown.

Example

integer ("1234") returns the integer value 1234.

Notes

This function operates only on strings and is not the same as the more general typecast operator.

See Also

typecast, _slang_guess_type, string, sprintf, char

13.10 isalnum, isalpha, isascii, isblank, iscntrl, isdigit, isgraph, islower, isprint, ispunct, isspace, isupper, isxdigit

Synopsis

Character classification functions

Usage

Char_Type isalnum(wch) Char_Type isalpha(wch) Char_Type isascii(wch) Char_Type isblank(wch) Char_Type iscntrl(wch) Char_Type isdigit(wch) Char_Type isgraph(wch) Char_Type islower(wch) Char_Type isprint(wch) Char_Type ispunct(wch) Char_Type isspace(wch) Char_Type isupper(wch) Char_Type isxdigit(wch)

Description

These functions return a non-zero value if the character given by wch is a member of the character class represented by the function, according to the table below. Otherwise, 0 will be returned to indicate that the character is not a member of the class. If the parameter wch is a string, then the first character (not necessarily a byte) of the string will be used.

   isalnum : alphanumeric character, equivalent to isalpha or isdigit
   isalpha : alphabetic character
   isascii : 7-bit unsigned ascii character
   isblank : space or a tab
   iscntrl : control character
   isdigit : digit 0-9
   isgraph : non-space printable character
   islower : lower-case character
   isprint : printable character, including a space
   ispunct : non-alphanumeric graphic character
   isspace : whitespace character (space, newline, tab, etc)
   isupper : uppercase case character
   isxdigit: hexadecimal digit character 0-9, a-f, A-F

See Also

strtrans

13.11 _slang_guess_type

Synopsis

Guess the data type that a string represents

Usage

DataType_Type _slang_guess_type (String_Type s)

Description

This function tries to determine whether its argument s represents an integer (short, int, long), floating point (float, double), or a complex number. If it appears to be none of these, then a string is assumed. It returns one of the following values depending on the format of the string s:

    Short_Type     :  short integer           (e.g., "2h")
    UShort_Type    :  unsigned short integer  (e.g., "2hu")
    Integer_Type   :  integer                 (e.g., "2")
    UInteger_Type  :  unsigned integer        (e.g., "2")
    Long_Type      :  long integer            (e.g., "2l")
    ULong_Type     :  unsigned long integer   (e.g., "2l")
    Float_Type     :  float                   (e.g., "2.0f")
    Double_Type    :  double                  (e.g., "2.0")
    Complex_Type   :  imaginary               (e.g., "2i")
    String_Type    :  Anything else.          (e.g., "2foo")
For example, _slang_guess_type("1e2") returns Double_Type but _slang_guess_type("e12") returns String_Type.

See Also

integer, string, double, atof, __is_datatype_numeric

13.12 string

Synopsis

Convert an object to a string representation.

Usage

String_Type string (obj)

Description

The string function may be used to convert an object obj of any type to its string representation. For example, string(12.34) returns "12.34".

Example

     define print_anything (anything)
     {
        message (string (anything));
     }

Notes

This function is not the same as typecasting to a String_Type using the typecast function.

See Also

typecast, sprintf, integer, char

13.13 tolower

Synopsis

Convert a character to lowercase.

Usage

Integer_Type lower (Integer_Type ch)

Description

This function takes an integer ch and returns its lowercase equivalent.

See Also

toupper, strup, strlow, int, char, define_case

13.14 toupper

Synopsis

Convert a character to uppercase.

Usage

Integer_Type toupper (Integer_Type ch)

Description

This function takes an integer ch and returns its uppercase equivalent.

See Also

tolower, strup, strlow, int, char, define_case

13.15 typecast

Synopsis

Convert an object from one data type to another.

Usage

typecast (x, new_type)

Description

The typecast function performs a generic typecast operation on x to convert it to new_type. If x represents an array, the function will attempt to convert all elements of x to new_type. Not all objects can be converted and a type-mismatch error will result upon failure.

Example

    define to_complex (x)
    {
       return typecast (x, Complex_Type);
    }
defines a function that converts its argument, x to a complex number.

See Also

int, double, typeof

13.16 _typeof

Synopsis

Get the data type of an object

Usage

DataType_Type _typeof (x)

Description

This function is similar to the typeof function except in the case of arrays. If the object x is an array, then the data type of the array will be returned. Otherwise _typeof returns the data type of x.

Example

    if (Integer_Type == _typeof (x))
      message ("x is an integer or an integer array");

See Also

typeof, array_info, _slang_guess_type, typecast

13.17 typeof

Synopsis

Get the data type of an object

Usage

DataType_Type typeof (x)

Description

This function returns the data type of x.

Example

  if (Integer_Type == typeof (x)) message ("x is an integer");

See Also

_typeof, is_struct_type, array_info, _slang_guess_type, typecast


Next Previous Contents