cs_convert

Description

Converts a data value from one datatype, locale, or format to another datatype, locale, or format.

Syntax

CS_RETCODE cs_convert(context, srcfmt, srcdata,
                      destfmt, destdata, resultlen)
 CS_CONTEXT           *context;
 CS_DATAFMT            *srcfmt;
 CS_VOID                   *srcdata;
 CS_DATAFMT           *destfmt;
 CS_VOID                  *destdata;
 CS_INT                     *resultlen;

Parameters

context

A pointer to a CS_CONTEXT structure.

srcfmt

A pointer to a CS_DATAFMT structure describing the source data format. The fields in *srcfmt are used as follows:

Field name

Set it to

datatype

A type constant representing the type of the source data (CS_CHAR_TYPE, CS_BINARY_TYPE, and so on).

maxlength

The length of the data in the *srcdata buffer. This value is ignored for fixed-length datatypes or if srcdata is NULL.

locale

A pointer to a CS_LOCALE structure containing localization values for the source data, or NULL to use localization values from *context.

All other fields are ignored.

For general information on the CS_DATAFMT structure, see the “CS_DATAFMT Structure” topics page in the Open Client Client-Library/C Reference Manual.

srcdata

A pointer to the source data. To indicate that the source data represents a null value, pass srcdata as NULL. If srcdata is NULL, cs_convert places the null substitution value for the datatype indicated by destfmt−>datatype in *destdata.

Table 2-15 lists the default null substitution value for each datatype. An application can define custom null substitution values by calling cs_setnull.

destfmt

A pointer to a CS_DATAFMT structure describing the destination data format. Table 2-2 lists the fields in *destfmt that are used.

Table 2-2: CS_DATAFMT fields for cs_convert’s *destfmt parameter

Field Name

Set it to

datatype

A type constant representing the desired destination datatype (CS_CHAR_TYPE, CS_BINARY_TYPE, and so on).

maxlength

The length of the destdata buffer.

locale

A pointer to a CS_LOCALE structure containing localization values for the destination data, or NULL to use localization values from *context.

format

A bit mask of the following symbols:

  • For character and text destinations only, use CS_FMT_NULLTERM to null-terminate the data, or CS_FMT_PADBLANK to pad to the full length of the variable with spaces.

  • For character, binary, text, and image destinations, use CS_FMT_PADNULL to pad to the full length of the variable with nulls.

  • When converting from a character source to a character destination, use CS_FMT_SAFESTR to double any occurrences of the single-quote character (') in the destination. CS_FMT_SAFESTR can be combined with CS_FMT_NULLTERM, CS_FMT_PADBLANK, or CS_FMT_PADNULL.

  • For any type of destination, use CS_FMT_UNUSED if no format information is being provided.

scale

The scale used for the destination variable.

If the source data is the same type as the destination, scale can be set to CS_SRC_VALUE to indicate that the destination should pick up its value for scale from the source data.

scale must be less than or equal to precision.

precision

The precision used for the destination variable.

If the source data is the same type as the destination, precision can be set to CS_SRC_VALUE to indicate that the destination should pick up its value for precision from the source data.

precision must be greater than or equal to scale.

All other fields are ignored.

For general information on the CS_DATAFMT structure, see the “CS_DATAFMT Structure” topics page in the Open Client Client-Library/C Reference Manual.

destdata

A pointer to the destination buffer space.

resultlen

A pointer to an integer variable. cs_convert sets *resultlen to the length, in bytes, of the data placed in *destdata. If the conversion fails, cs_convert sets *resultlen to CS_UNUSED.

resultlen is an optional parameter and can be passed as NULL.

Datatype Conversion Chart

The chart in Table 2-3 indicates which datatype conversions are supported by cs_convert. The source datatypes are listed in the leftmost column and the destination datatypes are listed in the top row of the chart. “X” indicates that the conversion is supported; a blank space indicates that the conversion is not supported.

Table 2-3: Datatype conversion chart

Open Client datatypes

CS_BINARY

CS_LNGBINARY

CS_VARBINARY

CS_BIT

CS_CHAR

CS_LONGCHAR

CS_VARCHAR

CS_DATETIME

CS_DATETIME4

CS_TINYINT

CS_SMALLINT

CS_INT

CS_DECIMAL

CS_NUMERIC

CS_FLOAT

CS_REAL

CS_MONEY

CS_MONEY4

CS_BOUNDARY

CS_SENSITIVITY

CS_TEXT

CS_IMAGE

CS_UNICHAR

CS_DATE

CS_TIME

CS_BIGINT

CS_USMALLINT

CS_UINT

CS_UBIGINT

CS_UNITEXT

CS_XML

CS_BINARY

CS_LONGBINARY

CS_VARBINARY

CS_BIT

CS_CHAR

CS_LONGCHAR

CS_VARCHAR

CS_DATETIME

CS_DATETIME4

CS_TINYINT

CS_SMALLINT

CS_INT

CS_DECIMAL

CS_NUMERIC

CS_FLOAT

CS_REAL

CS_MONEY

CS_MONEY4

CS_BOUNDARY

CS_SENSITIVITY

CS_TEXT

CS_IMAGE

CS_UNICHAR

CS_DATE

CS_TIME

CS_BIGINT

CS_USMALLINT

CS_UINT

CS_UBIGINT

CS_UNITEXT

CS_XML

Returns

Returns

Indicates

CS_SUCCEED

The routine completed successfully.

CS_FAIL

The routine failed.

A common reason for a cs_convert failure is that CS-Library does not support the requested conversion.

cs_convert conversion errors will generate CS-Library error messages. For more information about CS-Library error handling, see “Error handling”.

Usage


About specific conversions

NoteOpen Client and Open Server 15.0 support the unichar datatype. For information about this datatype, see Chapter 3, “Using Open Client and Open Server Datatypes”, in the Open Client Client-Library/C Programmers Guide.


Converting between character sets

See also

cs_conv_mult, cs_manage_convert, cs_set_convert, cs_setnull, cs_will_convert