Writing a program with non-ASCII characters🔗
In C++ and C, there are some common traps and pitfalls when programs containing string constants with non-ASCII characters are executed. The following ways to store the source files have different implications:
- Store source file in local 8-bit encoding
-
Only native characters can be stored. String constants that are created by the compiler are also encoded in local 8-bit encoding, that is, the execution character set is local 8-bit.
If strings in local 8-bit encoding are passed to HALCON/C or HALCON/C++, either- change the interface default encoding (by calling
SetHcInterfaceStringEncodingIsUtf8(false)orSetHcppInterfaceStringEncodingIsUtf8(false), respectively) or - create the string that is passed to HALCON with a suitable conversion function, for example,
HString::FromLocal8Bit().
- change the interface default encoding (by calling
-
When using HALCON/C++, the string constant can also be created as wide character string with
L.... - Store source file in UTF-8 with or without BOM signature
- The big advantage is that any Unicode characters can be stored.
To store files without BOM signature in Visual Studio, choose Unicode (UTF-8 without signature) - Codepage 65001 in the Save As or Advanced Save Options dialog.
Without BOM signature, the files are not automatically detected as UTF-8 files, that is, they are interpreted as local 8-bit files. Files with BOM signature marker are correctly read as UTF-8 files. However, the compiler converts the string constants automatically into local 8-bit encoding, that is, although the source character set is UTF-8, the execution character set is still local 8-bit. Hence, when passing these strings to HALCON, all points mentioned above are valid.
When using Visual Studio 2015 or later, it is possible to set the compiler option/utf-8. You can add this option in Project Property Page > Configuration Properties > C/C++ > All Options > Additional Options. If this option is set, both the source character set and the execution character set are UTF-8 (which works also without the BOM signature). In this case, all strings are UTF-8 and can directly be passed to HALCON.