简体   繁体   English

如何在C中引用UTF-16字符?

[英]How to refer to UTF-16 character in C?

Now I'm writing a C .header file for my library, which handles UTF-16 characters. 现在,我正在为我的库编写一个C .header文件,该文件处理UTF-16字符。

This .h should compile on Linux/Windows 32/64 bits in MSVC/GCC . 该.h应该在MSVC/GCC Linux/Windows 32/64 bitsMSVC/GCC Since it's a lib header, I cannot stick to C99 and later. 由于它是一个lib标头,因此我不能坚持使用C99和更高版本。 So I cannot use wchar_t or uint16_t . 所以我不能使用wchar_tuint16_t How can I specify a UTF-16 variable? 如何指定UTF-16变量?

So far I came to this: 到目前为止,我来到这里:

#if _WIN32 
  typedef wchar_t char_UTF16;
#else 
  #if __GNUC__
    typedef unsigned short char_UTF16;
  #else
    #error "Compiler not supported"
  #endif
#endif

But I really don't think this is the best solution. 但是我真的不认为这是最好的解决方案。

You can try some of the builtin types in a compiler-agnostic way: 您可以以与编译器无关的方式尝试某些内置类型:

#include <limits.h>
#include <wchar.h>

#if (WCHAR_MAX==65535) && WCHAR_MIN==0
typedef wchar_t char_UTF16;
#elif USHRT_MAX==65535
typedef unsigned short char_UTF16;
#elif UINT_MAX==65535
typedef unsigned char_UTF16;
#else
#    error "Cannot find 16-bit type"
#endif

(there's no point in trying unsigned long , since the standard requires it to be at least 32 bit wide) (尝试unsigned long是没有意义的,因为标准要求它必须至少为32位宽)

... although, I'm not really sure if you should even try wchar_t , probably I'd go straight for the numeric types, otherwise you risk having your clients assuming that eg wide char literals are of the "right" type for your library while in facts they are only on Windows. ...尽管我不确定,您是否还要尝试wchar_t ,可能我会直接使用数字类型,否则您可能会冒风险让您的客户假设例如,宽字符字面量对于您来说是“正确”类型库,而实际上它们仅在Windows上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM