How to write raw type / bytes to stdout?

风格不统一 提交于 2020-01-24 03:49:08

问题


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.


回答1:


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)

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.



来源:https://stackoverflow.com/questions/7422575/how-to-write-raw-type-bytes-to-stdout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!