【问题标题】:How to produce intentional encoding errors in u"..." strings?如何在 u"..." 字符串中产生有意的编码错误?
【发布时间】:2022-07-07 02:22:58
【问题描述】:

我正在编写一个 UTF-16 解码例程。为了检查它是否正常工作,我需要生成带有故意编码错误的测试字符串。但是,当我尝试以明显的方式在 C 中生成此类字符串时,编译器会拒绝我的代码,并显示“...不是有效的通用字符:”

u"\d800" /* unmatched low surrogate */
u"\dc01\d802" /* surrogates in wrong order */

如何生成带有故意编码错误的u"..." 字符串?

【问题讨论】:

    标签: c utf-16 string-literals


    【解决方案1】:

    \uXXXX\UXXXXXXXX 转义序列只能编码有效的通用字符。要编码其他 char16_t 值,请使用 \x... 转义序列:

    u"\xd800" /* unmatched low surrogate */
    u"\xdc01\xd802" /* surrogates in wrong order */
    

    【讨论】:

      猜你喜欢
      • 2011-11-27
      • 2013-07-02
      • 2015-03-15
      • 1970-01-01
      • 2013-08-16
      • 1970-01-01
      • 2017-03-08
      • 1970-01-01
      • 2020-12-26
      相关资源
      最近更新 更多