Search code examples
c++sql-serveroledb

Shouldn't VARCHAR data be converted to char * (MSSQL OLE DB)?


In a C++ program, I am trying to read data from MSSQL database using OLE DB. The column I am trying to read is a VARCHAR type. The data in the column is imported from a multi-value database. Sometimes the data in the column has a delimiter in it. The delimiter is a value marker (0Xfd). I covert the data read from the table to a char * like this:

retcode = WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)pDBColumnAccess[nCol].pData, -1, (char *)pReadBuf, pDBColumnAccess[nCol].cbDataLen, NULL, NULL);

Everything is fine if the data does not contain above mentioned delimiter - value marker (0xfd). But when the delimiter is there, in the converted data the value marker is replaced by some junk characters.

Shouldn't I do a conversion to char * in the case of VARCHAR? Is it enough to just copy the data as it is without any coversion?


Solution

  • The WideCharToMultiByte converts from UTF-16, yet there is no such thing as 0xFD character in UTF-16. All characters are encoded as at least 2 bytes. Did you actually mean 0x00FD (or even 0xFD00)?

    Also, UTF-8 (your "target" encoding since you specified CP_UTF8) does not guarantee that all characters will be encoded in just one byte.

    According to UTF Converter:

    • UTF-16 00FD converts to UTF-8 C3 BD.
    • UTF-16 FD00 converts to UTF-8 EF B4 80.

    Is that what you are getting?