Hacking inside print internals seems like a bad idea. Instead, I think you should execute a line that escapes itself, and end up using cat to print the line without any extra escaping.
You can use encodeString to do the initial escaping, gregexpr to identify octal \0.. screens, strtoi to convert strings representing octal numbers to these numbers, sprintf to print numbers in hexadecimal format and regenmatches to work with the corresponding details. The whole process will look something like this:
inputString <- "This is a \005 symbol. \x13 is \\x13." x <- encodeString(inputString) m <- gregexpr("\\\\[0-3][0-7][0-7]", x) charcodes <- strtoi(substring(regmatches(x, m)[[1]], 2, 4), 8) regmatches(x, m) <- list(sprintf("\\x%02x", charcodes)) cat(x, "\n")
Note that this approach will convert octal escape sequences such as \005 to hexadecimal escape sequences such as \x05 , but other escape sequences such as \t or \a will not be affected. You may need even more code to solve these problems, but the above should contain all the necessary ingredients.
Please note that the BSON specification you are referring to almost certainly meant raw bytes, so if your line contains a character with code 5, which you can write as "\x05" at your input, and you write this line to the desired output in binary mode, it shouldn't matter how R prints this line for you. After all, octal \005 and hexadecimal \x05 are just two representations of the same byte that you write.
Mvg
source share