【问题标题】:Comparing unicode and wchar_t C++ : `unable to find numeric literal operator 'operator""F'`比较 unicode 和 wchar_t C++:`无法找到数字文字运算符 'operator""F'`
【发布时间】:2016-03-04 18:46:10
【问题描述】:

所以我有一个wchar_t tempc;。我想看看它是否在 unicode 范围 000000-00007F 内。

这是我尝试比较值的方式:

if ((tempc < (wchar_t)000000) || (tempc > (wchar_t)00007F)) { ... 

我认为这会将 unicode 值 000000 和 00007F 转换为 wchar_t。但我收到以下错误:

error: invalid suffix "F" on integer constant
error: invalid digit "8" in octal constant
error: invalid suffix "FF" on integer constant
error: invalid digit "8" in octal constant 

很明显,编译器不会像 00007F 这样的值是 unicode。我在这里有什么误解?我是否先将它们转换为 unicode(如果是,如何转换?),然后转换为 wchar_t?

【问题讨论】:

  • 您应该使用 0x 前缀作为十六进制数字
  • 十六进制值以0x 开头。仅以 0 开头的值是八进制。
  • 成功了,谢谢。但我很困惑..“unicode”和“hexadecimal”是同义词吗?
  • Unicode 代码点值(通常)使用hexadecimal 数字系统编写。 unicode.org/charts
  • 你不必用十六进制写数字; 0x7F 也可以写成1270177。所以代码可能是(tempc &lt; (wchar_t)0 || tempc &gt; (wchar_t)127)(tempc &lt; (wchar_t)0 || tempc &gt; (wchar_t)0177)。但由于其他人只使用十六进制,因此最简单的方法是复制十六进制值,而不是进行数学运算以转换​​为十进制或八进制。

标签: c++ unicode wchar-t


【解决方案1】:

在 C++ 中,以 0 开头的常量值是八进制数。因此,它不能包含 8 也不能包含 F。十六进制常量应该写为 0x7F。随便写:

if ((tempc < (wchar_t)0) || (tempc > (wchar_t)0x7F)) { ... 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-12
    • 1970-01-01
    • 2021-03-30
    • 2017-02-23
    • 1970-01-01
    相关资源
    最近更新 更多