[R] print.raw - but convert ASCII?

Duncan Murdoch murdoch.duncan at gmail.com
Tue Apr 19 09:14:13 CEST 2011


On 11-04-18 9:51 PM, Matt Shotwell wrote:
> Does anyone know if there is a simple way to print raw vectors, such
> that ASCII characters are printed for bytes in the ASCII range, and
> their hex representation otherwise? rawToChar doesn't work when we have
> something like c(0x00, 0x00, 0x44, 0x00).

Do you really need hex?  rawToChar(x, multiple=TRUE) comes close, but 
displays using octal or symbolic escapes, e.g.

   [1] ""     "\001" "\002" "\003" "\004" "\005" "\006" "\a"   "\b" 
"\t"   "\n"
  [12] "\v"   "\f"   "\r"   "\016" "\017" "\020" "\021" "\022" "\023" 
"\024" "\025"
  [23] "\026" "\027" "\030" "\031" "\032" "\033" "\034" "\035" "\036" 
"\037" " "
  [34] "!"    "\""   "#"    "$"    "%"    "&"    "'"    "("    ")" 
"*"    "+"

If you really do want hex, then you'll need something like

ifelse( x < 32 | x >= 127, as.character(x), rawToChar(x, multiple=TRUE))

Duncan Murdoch



More information about the R-help mailing list