c - 参数错误: string contains null byte when use StringValueCStr

标签 c ruby ruby-c-extension

这是我的test file

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

运行以下代码时出现“ArgumentError:字符串包含空字节”:

在 ruby 方面:

get_article(File.read("test_file"))

在c端:

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

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

最佳答案

当我使用以下代码时:

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

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

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

在 ruby​​ 端或 c 端执行

关于c - 参数错误: string contains null byte when use StringValueCStr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34916024/

相关文章:

c - 我可以从 pic 18f4550 的 PORTBbits.RB7 看哪个值

ruby - 在一行中用 Ruby 中的默认值将哈希声明为常量

javascript - ruby on Rails 中使用 sweetalert 删除方法

ruby - Ruby block 是如何实现的(在编译器中)

objective-c - 将 Cocoa header 链接到 ruby​​ C 扩展

C scanf() 不解析我的输入

java - 删除最低位

ruby - 如何在 C(实际上是 swift)应用程序中完全重新初始化嵌入式 ruby​​ VM?

通过将数字替换为数字,将句子转换为数字。例如使用 strcmp 将 ABCD 转换为 2223

c - 如何在 Ruby 1.9 C 扩展中将 Block 转换为 Proc?