The rstoi() function
The rstoi() function converts a null-terminated string into a short integer value.
Syntax
mint rstoi(string, ival)
char *string;
mint *ival;
- string
- A pointer to a null-terminated string.
- ival
- A pointer to a mint value that holds the converted value.
Usage
The legal range of values is -32767
- 32767
.
The value -32768
is not valid because this value
is a reserved value that indicates null.
If string corresponds to a null integer, ival points to the representation for a SMALLINT null. To convert a string that corresponds to a long integer, use rstol(). Failure to do so can result in corrupted data representation.
Return codes
- =0
- The conversion was successful.
- !=0
- The conversion failed.
Example
This sample program is in the rstoi.ec file
in the demo directory.
/*
* rstoi.ec *
The following program tries to convert three strings to integers.
It displays the result of each conversion.
*/
#include <stdio.h>
EXEC SQL include sqltypes;
main()
{
mint err;
mint i;
short s;
printf("RSTOI Sample ESQL Program running.\n\n");
i = 0;
printf("Converting String 'abc':\n");
if((err = rstoi("abc", &i)) == 0)
printf("\tResult = %d\n\n", i);
else
printf("\tError %d in conversion of string #1\n\n", err);
i = 0;
printf("Converting String '32766':\n");
if((err = rstoi("32766", &i)) == 0)
printf("\tResult = %d\n\n", i);
else
printf("\tError %d in conversion of string #2\n\n", err);
i = 0;
printf("Converting String '':\n");
if((err = rstoi("", &i)) == 0)
{
s = i; /* assign to a SHORT variable */
if (risnull(CSHORTTYPE, (char *) &s)) /* and then test for NULL */
printf("\tResult = NULL\n\n");
else
printf("\tResult = %d\n\n", i);
}
else
printf("\tError %d in conversion of string #3\n\n", err);
printf("\nRSTOI Sample Program over.\n\n");
}
Output
RSTOI Sample ESQL Program running.
Converting String 'abc':
Error -1213 in conversion of string #1
Converting String '32766':
Result = 32766
Converting String '':
Result = NULL
RSTOI Sample Program over.