SQL to C: Binary

The identifiers for the binary ODBC SQL data types are:

SQL_BINARY
SQL_VARBINARY
SQL_LONGVARBINARY

The following table shows the ODBC C data types to which binary SQL data may be converted. For an explanation of the columns and terms in the table, see “Converting Data from SQL to C Data Types.”


C type identifier

Test

*TargetValuePtr
*StrLen_or
_IndPtr
SQL-
STATE
SQL_C_CHAR (Byte length of data) * 2 < BufferLength

(Byte length of data) * 2 >= BufferLength

Data

Truncated data

Length of data in bytes

Length of data in bytes

n/a

01004

SQL_C_WCHAR (Character length of data) * 2 < BufferLength

(Character length of data) * 2 >= BufferLength

Data

Truncated data

Length of data in characters

Length of data in characters

n/a

01004

SQL_C_BINARY Byte length of data <= BufferLength

Byte length of data > BufferLength

Data

Truncated data

Length of data in bytes

Length of data in bytes

n/a

01004


When binary SQL data is converted to character C data, each byte (8 bits) of source data is represented as two ASCII characters. These characters are the ASCII character representation of the number in its hexadecimal form. For example, a binary 00000001 is converted to “01” and a binary 11111111 is converted to “FF”.

The driver always converts individual bytes to pairs of hexadecimal digits and terminates the character string with a null byte. Because of this, if BufferLength is even and is less than the length of the converted data, the last byte of the *TargetValuePtr buffer is not used. (The converted data requires an even number of bytes, the next-to-last byte is a null byte, and the last byte cannot be used.)

Note   Application developers are discouraged from binding binary SQL data to a character C data type. This conversion is usually inefficient and slow.