Does VC have a compile option like '-fexec-charset' in GCC to set the execution character set?

前端 未结 4 2144
情话喂你
情话喂你 2020-12-28 10:22

GCC has -finput-charset, -fexec-charset and -fwide-exec-charset three compile options to specify particular encodings involved in a \"

4条回答
  •  滥情空心
    2020-12-28 10:58

    Credit on @user3998276 's answer and the great experiment.

    The conclusion tells me a lot

    • when meet L"string", wide string:

      • compiler first detects the cpp-file-encoding, then:
        • Unicode--> just use utf-16 // may here also has a conversion, like u8 to u16.
        • ACP--> convert the Unicode string to ACP
    • when meet "string", ordinary string literal:

      • compiler first detects the cpp-file-encoding, then
        • Unicode --> covert the Unicode character to ACP character
        • ACP --> just read the source file according ACP

    As to your problem, I think the 'insertion operations on db tables' is just a call to the db insertion API. So, all you need to do is to organize the command, like SQL, in UTF8. Once the API can understand your command, it can write the right value(imagine binary steam) for you.

    Try:

    • In c++11 and later, you can specify utf-8 string by prefix 'u8', like

    u8"INSERT INTO table_name (col1, col2,...) VALUES (v1, v2,....)"

    http://en.cppreference.com/w/cpp/language/string_literal

    • Use a third-party string wrapper, like QString from QT.

      First wrap your SQL to QString, then it can be easily convert to utf8, QByteArray x = mySql.toUtf8(). The QByteArray is just 'array of byte', so you can static_cast it to the type the insertion API wants.

    Again, read the answer of @user3998276 carefully, you may need to change the encoding of your cpp file to Unicode if there are some character cannot be represent in you ANSI code page.

提交回复
热议问题