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 1015117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:21:11+00:00 2026-05-16T10:21:11+00:00

I need a function like string ToLiteral(string input) from this post . Such that

  • 0

I need a function like string ToLiteral(string input) from this post. Such that

char *literal = to_literal("asdf\r\n");

Would yield literal ==> “asdf\\r\\n”.

I’ve googled around, but not been able to find anything (guess that I’ve must be using the wrong terms). However, I assume that a library with this functionality must be out there somewhere…

Thank you for the interresting answers. Googling “c string escape function” by the way seems to be the key to obtaining even more examples and GLIB provides g_strescape () which seems to be exactly what I need.

  • 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-05-16T10:21:12+00:00Added an answer on May 16, 2026 at 10:21 am

    There’s no built-in function for this, but you could whip one up:

    /* Expands escape sequences within a C-string
     *
     * src must be a C-string with a NUL terminator
     *
     * dest should be long enough to store the resulting expanded
     * string. A string of size 2 * strlen(src) + 1 will always be sufficient
     *
     * NUL characters are not expanded to \0 (otherwise how would we know when
     * the input string ends?)
     */
    
    void expand_escapes(char* dest, const char* src) 
    {
      char c;
    
      while (c = *(src++)) {
        switch(c) {
          case '\a': 
            *(dest++) = '\\';
            *(dest++) = 'a';
            break;
          case '\b': 
            *(dest++) = '\\';
            *(dest++) = 'b';
            break;
          case '\t': 
            *(dest++) = '\\';
            *(dest++) = 't';
            break;
          case '\n': 
            *(dest++) = '\\';
            *(dest++) = 'n';
            break;
          case '\v': 
            *(dest++) = '\\';
            *(dest++) = 'v';
            break;
          case '\f': 
            *(dest++) = '\\';
            *(dest++) = 'f';
            break;
          case '\r': 
            *(dest++) = '\\';
            *(dest++) = 'r';
            break;
          case '\\': 
            *(dest++) = '\\';
            *(dest++) = '\\';
            break;
          case '\"': 
            *(dest++) = '\\';
            *(dest++) = '\"';
            break;
          default:
            *(dest++) = c;
         }
      }
    
      *dest = '\0'; /* Ensure nul terminator */
    }
    

    Note that I’ve left out translation of an escape sequence for the “escape” character, since this isn’t standardized in C (some compilers use \e and others use \x). You can add in whichever applies to you.

    If you want a function that allocates your destination buffer for you:

    /* Returned buffer may be up to twice as large as necessary */
    char* expand_escapes_alloc(const char* src)
    {
       char* dest = malloc(2 * strlen(src) + 1);
       expand_escapes(dest, src);
       return dest;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand the need for a function like DB_Get_Cached(sql string) that hashes the SQL
I want to create a function like this... [AcceptVerbs(HttpVerbs.Post)] public ActionResult SaveImage(string file, string
Basically I have a function that roughly looks like this and I need to
I have a C# function that look like this: bool func(string name, bool retry)
I'd like to run a preg_replace PHP function on a string but I need
I need a function like var_dump($object) in php The problem is that the Var_dump
I have something like this: function showFunction () { // need position and place
I need to export my nodes like this: function recursive_simplify(node){ if(node.children){ for(var i =0;i<node.children.length;i++){
I need to create a function which rounds decimal numbers like this: Round($32.95, 0)
I need to pass multiple arguments to a function that I would like to

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.