SBCS 和 MBCS 数据类型SBCS and MBCS Data Types

仅处理一个多字节字符或多字节字符的一个字节的任何 Microsoft MBCS 运行库例程需要一个 unsigned int 参数(其中 0x00 <= 字符值 <= 0xFFFF,0x00 <= 字节值 <= 0xFF)。Any Microsoft MBCS run-time library routine that handles only one multibyte character or one byte of a multibyte character expects an unsigned int argument (where 0x00 <= character value <= 0xFFFF and 0x00 <= byte value <= 0xFF ). 处理字符串上下文中的多字节的字节或字符的 MBCS 例程需要一个用 unsigned char 指针表示的多字节字符串。An MBCS routine that handles multibyte bytes or characters in a string context expects a multibyte-character string to be represented as an unsigned char pointer.

小心

多字节字符的每个字节可用一个 8 位 char 表示。Each byte of a multibyte character can be represented in an 8-bit char. 然而,带有大于 0x7F 的值的 SBCS 类型的 MBCSchar 单字节字符是负的。However, an SBCS or MBCS single-byte character of type char with a value greater than 0x7F is negative. 将此类字符直接转换为 intlong 时,结果将由编译器进行符号扩展,因此可能产生意外的结果。When such a character is converted directly to an int or a long, the result is sign-extended by the compiler and can therefore yield unexpected results.

因此,最好将多字节字符的一个字节表示为一个 8 位 unsigned charTherefore it is best to represent a byte of a multibyte character as an 8-bit unsigned char. 或者,为避免产生负数结果,可在将 char 类型的单字节字符转换为 unsigned charint 之前将其转换为 longOr, to avoid a negative result, simply convert a single-byte character of type char to an unsigned char before converting it to an int or a long.

由于某些 SBCS 字符串处理函数采用(带符号的)char* 参数,因此定义 _MBCS 时,编译器会发出类型不匹配的警告。Because some SBCS string-handling functions take (signed) char* parameters, a type mismatch compiler warning will result when _MBCS is defined. 可通过三种方法来避免此警告(按效率高低的顺序列出):There are three ways to avoid this warning, listed in order of efficiency:

  1. 使用 TCHAR.H 中的“类型安全的”内联函数。Use the "type-safe" inline functions in TCHAR.H. 这是默认行为。This is the default behavior.

  2. 通过在命令行上定义 _MB_MAP_DIRECT,使用 TCHAR.H 中的“直接的”宏。Use the "direct" macros in TCHAR.H by defining _MB_MAP_DIRECT on the command line. 如果执行此操作,必须手动匹配类型。If you do this, you must manually match types. 这是最快的方法,但不是类型安全的方法。This is the fastest method but is not type-safe.

  3. 使用 TCHAR.H 中“类型安全的”静态链接的库函数。Use the "type-safe" statically linked library functions in TCHAR.H. 为此,在命令行上定义常量 _NO_INLININGTo do so, define the constant _NO_INLINING on the command line. 这是最慢的方法,但是最能确保类型安全。This is the slowest method, but the most type-safe.

另请参阅See Also

国际化 Internationalization
按类别分的运行时例程Run-Time Routines by Category