如何将原始类型/字节写入标准输出?
我在将原始类型输出到标准输出方面挣扎了很长一段时间。 这是我尝试过的方法,但没有按预期的方式工作:
r <- as.raw(c(0x41, 0x00, 0x43)) # r = "A\0C"
cat(rawToChar(r)) # displays warning and skips data after NULL (outputs "A")
cat(r) # outputs "41 00 43"
writeBin(r, stdout()) # error: can only write to binary connection
我正在寻找一种将所有三个字节/字符打印到标准输出的方法。
I am struggling for quite some time with outputting a raw type to standard output.
Here is what I tried and did not work the desired way:
r <- as.raw(c(0x41, 0x00, 0x43)) # r = "A\0C"
cat(rawToChar(r)) # displays warning and skips data after NULL (outputs "A")
cat(r) # outputs "41 00 43"
writeBin(r, stdout()) # error: can only write to binary connection
I am looking for a way to get all three bytes / characters printed to stdout.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用的操作系统具有“cat”或类似程序,我们可以将任意数据通过管道传输到标准输出,如下所示:
这已经成为一个问题一段时间了,特别是因为我们想使用 R 作为通用网关接口(CGI )。我不相信有更直接的路线,但您可以查看 RApache 源代码,看看 < code>sendBin 函数已实现。
If you are using an operating system that has a 'cat' or similar program, we can pipe arbitrary data to stdout like so:
This has been an issue for some time, especially because we would like to use R for common gateway interface (CGI). I don't believe there is a more direct route, but you might look at the RApache source code, to see how the
sendBin
function is implemented.