如何将原始类型/字节写入stdout? [英] How to write raw type / bytes to stdout?
问题描述
我在将原始类型输出到标准输出方面苦苦挣扎了很长时间. 这是我尝试过但未按预期方式工作的内容:
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
我正在寻找一种将所有三个字节/字符打印到stdout的方法.
I am looking for a way to get all three bytes / characters printed to stdout.
推荐答案
如果您使用的操作系统带有猫"或类似程序,我们可以将任意数据通过管道传输到stdout,如下所示:
If you are using an operating system that has a 'cat' or similar program, we can pipe arbitrary data to stdout like so:
con <- pipe("cat", "wb")
writeBin(as.raw(c(0x41, 0x00, 0x43)), con)
flush(con)
这一直是一个问题,特别是因为我们想将R用于通用网关接口(CGI).我认为没有更直接的途径,但是您可以查看 RApache 源代码,以了解sendBin
功能已实现.
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.
这篇关于如何将原始类型/字节写入stdout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!