Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8353253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:12:10+00:00 2026-06-09T09:12:10+00:00

Not sure I am using the right terminology here, but I need the print

  • 0

Not sure I am using the right terminology here, but I need the print or deparse methods use C notation (e.g. "\x05" instead of "\005" ) when escaping bytes out of the regular character set.

x <- "This is a \x05 symbol"
print(x)
[1] "This is a \005 symbol"

Is there a native way to accomplish this?

I need this for generating BSON: http://bsonspec.org/#/specification. All of the examples explicitly use \x05 notation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T09:12:11+00:00Added an answer on June 9, 2026 at 9:12 am

    Hacking into the internals of print seems a bad idea. Instead I think you should do the string escaping yourself, and eventually use cat to print the string without any extra escaping.

    You can use encodeString to do the initial escaping, gregexpr to identify octal \0.. escapes, strtoi to convert strings representing octal numbers to those numbers, sprintf to print numbers in hexadecimal, and regenmatches to operate on the matched parts. The whole process would 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 escapes like \005 to hexadecimal escapes like \x05, but other escape sequences like \t or \a won’t be affected by this. You might need more code to deal with those as well, but the above should contain all the ingredients you need.

    Note that the BSON specification you refer to almost certainly meant raw bytes, so as long as your string contains a character with code 5, which you can write as "\x05" in your input, and you write that string to the desired output in binary mode, it shouldn’t matter at all how R prints that string to you. After all, octal \005 and hexadecimal \x05 are just two representations of the same byte you’ll write.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using JPA but I'm not sure how to use it for relation between
I'm not sure if this is the right place to ask, but here goes...
I'm not sure if I have seen this right but using the jQuery UI
I'm not sure if user metrics is necessarily the right term to use but
I was not sure if it was me, that was not using the right
I'm not sure I am using ad-get-args and ad-get-arg right. For example, the following
Not sure if I am using the correct term, but it's not child/parent since
I am using Drupal for a project but am not sure on the best
I am using D-Day calendar and I am not sure but I got a
I'm not sure if I'm using the correct terminology, so please correct me if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.