【问题标题】:ArgumentError: string contains null byte when use StringValueCStrArgumentError:使用 StringValueCStr 时字符串包含空字节
【发布时间】:2016-01-21 05:26:48
【问题描述】:

这是我的test file

这是我的完整代码 (github.com/roroco/ro_article/tree/string-contains-null-byte)

运行以下代码时,我得到“ArgumentError: string contains null byte”:

在红宝石方面:

get_article(File.read("test_file"))

在c端:

VALUE get_article(VALUE self, VALUE html) {
    str html2 = StringValueCStr(html);

test file 这么大,如何在这个文件中找到空字节?以及如何使 StringValueCStr 适用于所有字符串,即使它包含“空字节”

【问题讨论】:

    标签: c ruby ruby-c-extension


    【解决方案1】:

    当我使用以下代码时:

    body = File.read("test_file")
    result = body.inspect
    

    我的“结果”包含“\u0000”(我的编码是utf8),所以解决方法是

    body.gsub(%r{\u0000}, "")
    

    在 ruby​​ 端或 c 端进行

    【讨论】:

      猜你喜欢
      • 2012-08-08
      • 1970-01-01
      • 2016-01-24
      • 1970-01-01
      • 1970-01-01
      • 2015-06-01
      • 2020-06-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多