<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<TITLE> S-Lang Run-Time Library Reference: Version 1.4.0: Data-Type Conversion Functions</TITLE>
<LINK HREF="slangfun-10.html" REL=next>
<LINK HREF="slangfun-8.html" REL=previous>
<LINK HREF="slangfun.html#toc9" REL=contents>
</HEAD>
<BODY>
<A HREF="slangfun-10.html">Next</A>
<A HREF="slangfun-8.html">Previous</A>
<A HREF="slangfun.html#toc9">Contents</A>
<HR>
<H2><A NAME="s9">9. Data-Type Conversion Functions</A></H2>

<P>
<H2><A NAME="_slang_guess_type"></A> <A NAME="ss9.1">9.1 <B>_slang_guess_type</B></A>
</H2>

<P>
<DL>
<DT><B> Synopsis </B><DD><P>Guess the data type that a string represents.
<DT><B> Usage </B><DD><P><CODE>DataType_Type _slang_guess_type (String_Type s)</CODE>
<DT><B> Description </B><DD><P>This function tries to determine whether its argument <CODE>s</CODE> represents
an integer or a floating point number.  If it appears to be neither,
then a string is assumed.  It returns one of three values depending on
the format of the string <CODE>s</CODE>:
<BLOCKQUOTE><CODE>
<PRE>
    Integer_Type     :   If it appears to be an integer
    Double_Type      :   If it appears to be a double
    String_Type      :   Anything else.
</PRE>
</CODE></BLOCKQUOTE>

For example, <CODE>_slang_guess_type("1e2")</CODE> returns
<CODE>Double_Type</CODE> but <CODE>_slang_guess_type("e12")</CODE> returns
<CODE>String_Type</CODE>.
<DT><B> See Also </B><DD><P><CODE>integer, string, double</CODE>
</DL>
<P>
<P>
<H2><A NAME="_typeof"></A> <A NAME="ss9.2">9.2 <B>_typeof</B></A>
</H2>

<P>
<DL>
<DT><B> Synopsis </B><DD><P>Get the data type of an object
<DT><B> Usage </B><DD><P><CODE>DataType_Type _typeof (x)</CODE>
<DT><B> Description </B><DD><P>This function is similar to the <CODE>typeof</CODE> function except in the
case of arrays.  If the object <CODE>x</CODE> is an array, then the data
type of the array will be returned. otherwise <CODE>_typeof</CODE> returns
the data type of <CODE>x</CODE>. 
<DT><B> Example </B><DD><P>
<BLOCKQUOTE><CODE>
<PRE>
  if (Integer_Type == _typeof (x)) 
    message ("x is an integer or an integer array");
</PRE>
</CODE></BLOCKQUOTE>
<DT><B> See Also </B><DD><P><CODE>typeof, array_info, _slang_guess_type, typecast</CODE>
</DL>
<P>
<P>
<H2><A NAME="atof"></A> <A NAME="ss9.3">9.3 <B>atof</B></A>
</H2>

<P>
<DL>
<DT><B> Synopsis </B><DD><P>Convert a string to a double precision number
<DT><B> Usage </B><DD><P><CODE>Double_Type atof (String_Type s)</CODE>
<DT><B> Description </B><DD><P>This function converts a string <CODE>s</CODE> to a double precision value
and returns the result.  It performs no error checking on the format
of the string.  The function <CODE>_slang_guess_type</CODE> may be used to
check the syntax of the string.
<DT><B> Example </B><DD><P>
<BLOCKQUOTE><CODE>
<PRE>
     define error_checked_atof (s)
     {
        switch (_slang_guess_type (s))
        {
           case Double_Type:
             return atof (s);
        }
        {
           case Integer_Type:
             return double (integer (s));
        }

        verror ("%s is is not a double", s);
    }
</PRE>
</CODE></BLOCKQUOTE>
<DT><B> See Also </B><DD><P><CODE>typecast, double, _slang_guess_type</CODE>
</DL>
<P>
<P>
<H2><A NAME="char"></A> <A NAME="ss9.4">9.4 <B>char</B></A>
</H2>

<P>
<DL>
<DT><B> Synopsis </B><DD><P>Convert an ascii value into a string
<DT><B> Usage </B><DD><P><CODE>String_Type char (Integer_Type c)</CODE>
<DT><B> Description </B><DD><P>The <CODE>char</CODE> function converts an integer ascii value <CODE>c</CODE> to a string
of unit length such that the first character of the string is <CODE>c</CODE>.
For example, <CODE>char('a')</CODE> returns the string <CODE>"a"</CODE>.
<DT><B> See Also </B><DD><P><CODE>integer, string, typedef</CODE>
</DL>
<P>
<P>
<H2><A NAME="define_case"></A> <A NAME="ss9.5">9.5 <B>define_case</B></A>
</H2>

<P>
<DL>
<DT><B> Synopsis </B><DD><P>Define upper-lower case conversion.
<DT><B> Usage </B><DD><P><CODE>define_case (Integer_Type ch_up, Integer_Type ch_low);</CODE>
<DT><B> Description </B><DD><P>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 <CODE>ch_up</CODE> is the ascii value of the uppercase character
and the second parameter <CODE>ch_low</CODE> is the ascii value of its
lowercase counterpart.
<DT><B> See Also </B><DD><P><CODE>strlow, strup</CODE>
</DL>
<P>
<P>
<H2><A NAME="double"></A> <A NAME="ss9.6">9.6 <B>double</B></A>
</H2>

<P>
<DL>
<DT><B> Synopsis </B><DD><P>Convert an object to double precision
<DT><B> Usage </B><DD><P><CODE>result = double (x)</CODE>
<DT><B> Description </B><DD><P>The <CODE>double</CODE> function typecasts an object <CODE>x</CODE> to double
precision.  For example, if <CODE>x</CODE> is an array of integers, an
array of double types will be returned.  If an object cannot be
converted to <CODE>Double_Type</CODE>, a type-mismatch error will result.
<DT><B> Notes </B><DD><P>The <CODE>double</CODE> function is equivalent to the typecast operation
<BLOCKQUOTE><CODE>
<PRE>
     typecast (x, Double_Type)
</PRE>
</CODE></BLOCKQUOTE>

To convert a string to a double precision number, use <CODE>atoi</CODE>
function.
<DT><B> See Also </B><DD><P><CODE>typecast, atoi, int</CODE>
</DL>
<P>
<P>
<H2><A NAME="int"></A> <A NAME="ss9.7">9.7 <B>int</B></A>
</H2>

<P>
<DL>
<DT><B> Synopsis </B><DD><P>Typecast an object to an integer
<DT><B> Usage </B><DD><P><CODE>int (s)</CODE>
<DT><B> Description </B><DD><P>This function performs a typecast of <CODE>s</CODE> from its data type to
an object of <CODE>Integer_Type</CODE>.  If <CODE>s</CODE> is a string, it returns
returns the ascii value value of the first character of the string
<CODE>s</CODE>.  If <CODE>s</CODE> is <CODE>Double_Type</CODE>, <CODE>int</CODE> truncates the
number to an integer and returns it.
<DT><B> Example </B><DD><P><CODE>int</CODE> can be used to convert single character strings to
integers.  As an example, the intrinsic function <CODE>isdigit</CODE> may
be defined as
<BLOCKQUOTE><CODE>
<PRE>
    define isdigit (s)
    {
      if ((int (s) &gt;= '0') and (int (s) &lt;= '9')) return 1;
      return 0;
    }
</PRE>
</CODE></BLOCKQUOTE>
<DT><B> Notes </B><DD><P>This function is equalent to <CODE>typecast (s, Integer_Type)</CODE>;
<DT><B> See Also </B><DD><P><CODE>typecast, double, integer, char, isdigit</CODE>
</DL>
<P>
<P>
<H2><A NAME="integer"></A> <A NAME="ss9.8">9.8 <B>integer</B></A>
</H2>

<P>
<DL>
<DT><B> Synopsis </B><DD><P>Convert a string to an integer
<DT><B> Usage </B><DD><P><CODE>Integer_Type integer (String_Type s)</CODE>
<DT><B> Description </B><DD><P>The <CODE>integer</CODE> function converts a string representation of an
integer back to an integer.  If the string does not form a valid
integer, a type-mismatch error will be generated.
<DT><B> Example </B><DD><P><CODE>integer ("1234")</CODE> returns the integer value <CODE>1234</CODE>.
<DT><B> Notes </B><DD><P>This function operates only on strings and is not the same as the
more general <CODE>typecast</CODE> operator.
<DT><B> See Also </B><DD><P><CODE>typecast, _slang_guess_type, string, sprintf, char</CODE>
</DL>
<P>
<P>
<H2><A NAME="isdigit"></A> <A NAME="ss9.9">9.9 <B>isdigit</B></A>
</H2>

<P>
<DL>
<DT><B> Synopsis </B><DD><P>Tests for a decimal digit character
<DT><B> Usage </B><DD><P><CODE>Integer_Type isdigit (String_Type s)</CODE>
<DT><B> Description </B><DD><P>This function returns a non-zero value if the first character in the
string <CODE>s</CODE> is a digit; otherwise, it returns zero.
<DT><B> Example </B><DD><P>A simple, user defined implementation of <CODE>isdigit</CODE> is
<BLOCKQUOTE><CODE>
<PRE>
    define isdigit (s)
    {
       return ((s[0] &lt;= '9') and (s[0]  &gt;= '0'));
    }
</PRE>
</CODE></BLOCKQUOTE>

However, the intrinsic function <CODE>isdigit</CODE> executes many times faster
than the equivalent representation defined above.
<DT><B> Notes </B><DD><P>Unlike the C function with the same name, the <B>S-lang</B> function takes
a string argument.
<DT><B> See Also </B><DD><P><CODE>int, integer</CODE>
</DL>
<P>
<P>
<H2><A NAME="string"></A> <A NAME="ss9.10">9.10 <B>string</B></A>
</H2>

<P>
<DL>
<DT><B> Synopsis </B><DD><P>Convert an object to a string representation.
<DT><B> Usage </B><DD><P><CODE>Integer_Type string (obj)</CODE>
<DT><B> Description </B><DD><P>The <CODE>string</CODE> function may be used to convert an object
<CODE>obj</CODE> of any type to a string representation.
For example, <CODE>string(12.34)</CODE> returns <CODE>"12.34"</CODE>.
<DT><B> Example </B><DD><P>
<BLOCKQUOTE><CODE>
<PRE>
     define print_anything (anything)
     {
        message (string (anything));
     }
</PRE>
</CODE></BLOCKQUOTE>
<DT><B> Notes </B><DD><P>This function is <EM>not</EM> the same as typecasting to a <CODE>String_Type</CODE>
using the <CODE>typecast</CODE> function.
<DT><B> See Also </B><DD><P><CODE>typecast, sprintf, integer, char</CODE>
</DL>
<P>
<P>
<H2><A NAME="tolower"></A> <A NAME="ss9.11">9.11 <B>tolower</B></A>
</H2>

<P>
<DL>
<DT><B> Synopsis </B><DD><P>Convert a character to lowercase.
<DT><B> Usage </B><DD><P><CODE>Integer_Type lower (Integer_Type ch)</CODE>
<DT><B> Description </B><DD><P>This function takes an integer <CODE>ch</CODE> and returns its lowercase
equivalent.
<DT><B> See Also </B><DD><P><CODE>toupper, strup, strlow, int, char, define_case</CODE>
</DL>
<P>
<P>
<H2><A NAME="toupper"></A> <A NAME="ss9.12">9.12 <B>toupper</B></A>
</H2>

<P>
<DL>
<DT><B> Synopsis </B><DD><P>Convert a character to uppercase.
<DT><B> Usage </B><DD><P><CODE>Integer_Type toupper (Integer_Type ch)</CODE>
<DT><B> Description </B><DD><P>This function takes an integer <CODE>ch</CODE> and returns its uppercase
equivalent.
<DT><B> See Also </B><DD><P><CODE>tolower, strup, strlow, int, char, define_case</CODE>
</DL>
<P>
<P>
<H2><A NAME="typecast"></A> <A NAME="ss9.13">9.13 <B>typecast</B></A>
</H2>

<P>
<DL>
<DT><B> Synopsis </B><DD><P>Convert an object from one data type to another.
<DT><B> Usage </B><DD><P><CODE>typecast (x, new_type)</CODE>
<DT><B> Description </B><DD><P>The <CODE>typecast</CODE> function performs a generic typecast operation on
<CODE>x</CODE> to convert it to <CODE>new_type</CODE>.  If <CODE>x</CODE> represents an
array, the function will attempt to convert all elements of <CODE>x</CODE>
to <CODE>new_type</CODE>.  Not all objects can be converted and a
type-mismatch error will result upon failure.
<DT><B> Example </B><DD><P>
<BLOCKQUOTE><CODE>
<PRE>
    define to_complex (x)
    {
       return typecast (x, Complex_Type);
    }
</PRE>
</CODE></BLOCKQUOTE>

defines a function that converts its argument, <CODE>x</CODE> to a complex
number.
<DT><B> See Also </B><DD><P><CODE>int, double, typeof</CODE>
</DL>
<P>
<P>
<H2><A NAME="typeof"></A> <A NAME="ss9.14">9.14 <B>typeof</B></A>
</H2>

<P>
<DL>
<DT><B> Synopsis </B><DD><P>Get the data type of an object.
<DT><B> Usage </B><DD><P><CODE>DataType_Type typeof (x)</CODE>
<DT><B> Description </B><DD><P>This function returns the data type of <CODE>x</CODE>.
<DT><B> Example </B><DD><P>
<BLOCKQUOTE><CODE>
<PRE>
  if (Integer_Type == typeof (x)) message ("x is an integer");
</PRE>
</CODE></BLOCKQUOTE>
<DT><B> See Also </B><DD><P><CODE>_typeof, is_struct_type, array_info, _slang_guess_type, typecast</CODE>
</DL>
<P>
<P>
<P>
<HR>
<A HREF="slangfun-10.html">Next</A>
<A HREF="slangfun-8.html">Previous</A>
<A HREF="slangfun.html#toc9">Contents</A>
</BODY>
</HTML>
