如何将C字符串转换成在C逃脱它的版本? [英] How to convert a c string into its escaped version in c?

查看:94
本文介绍了如何将C字符串转换成在C逃脱它的版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有buildin的功能或逃避 C 字符数组,如果用如的printf 应再次屈服原来的字符数组。

 的char *海峡=\\ tHello世界\\ n;
字符* escaped_str =逃生(STR); //应该包含\\ tHello世界\\ N用char \\吨。
的printf(escaped_str); //应该打印出[TAB]的Hello World [nextline]类似,如果是印海峡。

有没有在C的简单方式来逃避与C转义字符的字符串。

更新

我有缓冲含有转义字符的字符串。我想在C文件中包含。对于我需要逃避它,因此它可以得到遵守。我只需要知道是否有这样做,而不是在扫描\\ n \\ t等缓冲区,并产生有C转义字符的简单方法。

 的for(int i = 0; I< strlen的(缓冲);我++)
    如果(缓冲[I] =='\\ n')
      sprintf的(DEST,\\\\ N)
    否则....

更新2

我写了这个功能。它做工精细。

 的char *逃逸(字符*缓冲区){
    INT I,J;
    int类型l = strlen的(缓冲器)+ 1;
    炭esc_char [] = {'\\一','\\ B','\\ F'的'\\ n','\\ r',即'\\ t'的'\\ V','\\\\'};
    CHAR essc_str [] = {'A​​','B','F','N','R','T','V','\\\\'};
  字符* DEST =(字符*)释放calloc(L * 2,sizeof的(炭));
    字符* PTR = DEST;
    对于(i = 0; I<升;我++){
        为(J = 0; J&下; 8; J ++){
            如果(缓冲[I] == esc_char [J]){
              * PTR ++ ='\\\\';
              * PTR ++ = essc_str [J]。
                 打破;
            }
        }
        如果(J == 8)
      * PTR ++ =缓冲[I]
    }
  * PTR ='\\ 0';
    返回DEST;
}


解决方案

没有,没有创造字符串的源$ C ​​$ C版本的标准功能。但是你可以使用 iscntrl判断函数写一个,或者只是使用开关关键字。

但是,除非你的程序中写出旨在通过编译运行的C源文件,你并不需要转义字符串工作。 的printf 不处理字符转义序列,唯一的变量插入(%d个%S 等)

具体地讲,下面的产生相同的输出:

 的printf(\\ tHello世界\\ n);

 为const char *海峡=\\ tHello世界\\ n;
的printf(STR);

 为const char *海峡=\\ tHello世界\\ n;
的printf(%S,STR);

第二个是不是一个好主意,因为如果 STR 包含你的程序会产生不好的输出并可能崩溃。

编辑:对于生产源头code版本,有几个办法:

简单

,但不太可读的输出:

 如果(isctrl(CH)|| CH =='\\\\'|| CH =='\\'|| CH =='\\''){
   fprintf中(OUTF,\\\\ X%02X,CH);
}
其他
   的fputc(CH,OUTF);

更可读的结果:

 开关(CH){
  案件 '\\':
    的fputs(\\\\\\,OUTF);
    打破;
  案件 '\\'':
    的fputs(\\\\\\',OUTF);
    打破;
  案件 '\\\\':
    的fputs(\\\\\\\\,OUTF);
    打破;
  案\\ A:
    的fputs(\\\\一个,OUTF);
    打破;
  案例'\\ B':
    的fputs(\\\\ B,OUTF);
    打破;
  情况下的'\\ n':
    的fputs(\\\\ N,OUTF);
    打破;
  案\\ t:
    的fputs(\\\\ T,OUTF);
    打破;
  // 等等
  默认:
    的fputc(CH,OUTF);
}

Is there any buildin function or a alternative simple and fast way of escape a C character array that if used with e.g printf should yield original character array again.

char* str = "\tHello World\n";
char* escaped_str = escape(str); //should contain "\tHello World\n" with char \ ,t.
printf(escaped_str); //should print out [TAB]Hello World[nextline] similar to if str was printed.

Is there a simple way in c to escape a string with c escape characters.

Update

I have buffer containing a string with escape character. And i want to include in a C file. For that i need to escape it so it can be complied. I just need to know if there is simple way of doing it instead of scanning the buffer for \n \t etc and generating there c escape char.

for(int i=0; i< strlen(buffer);i++)
    if(buffer[i]=='\n')
      sprintf(dest,"\\n")
    else ....

Update 2

I wrote this function. It work fine.

char* escape(char* buffer){
    int i,j;
    int l = strlen(buffer) + 1;
    char esc_char[]= { '\a','\b','\f','\n','\r','\t','\v','\\'};
    char essc_str[]= {  'a', 'b', 'f', 'n', 'r', 't', 'v','\\'};
  char* dest  =  (char*)calloc( l*2,sizeof(char));
    char* ptr=dest;
    for(i=0;i<l;i++){
        for(j=0; j< 8 ;j++){
            if( buffer[i]==esc_char[j] ){
              *ptr++ = '\\';
              *ptr++ = essc_str[j];
                 break;
            }
        }
        if(j == 8 )
      *ptr++ = buffer[i];
    }
  *ptr='\0';
    return dest;
}

解决方案

No, there isn't any standard function for creating the source code version of the string. But you could use the iscntrl function to write one, or just use the switch keyword.

But, unless your program writes out a C source file intended to be run through the compiler, you don't need to work with escaped strings. printf doesn't process character escape sequences, only variable insertions (%d, %s, etc)

Specifically, the following produce the same output:

printf("\tHello World\n");

and

const char* str = "\tHello World\n";
printf(str);

and

const char* str = "\tHello World\n";
printf("%s", str);

The second one isn't a good idea, because if str contained % your program would produce bad output and could crash.

EDIT: For producing the source code version, there are a couple of approaches:

Simpler, but less readable output:

if (isctrl(ch) || ch == '\\' || ch == '\"' || ch == '\'') {
   fprintf(outf, "\\x%02x", ch);
}
else
   fputc(ch, outf);

More readable results:

switch (ch) {
  case '\"':
    fputs("\\\"", outf);
    break;
  case '\'':
    fputs("\\\'", outf);
    break;
  case '\\':
    fputs("\\\\", outf);
    break;
  case '\a':
    fputs("\\a", outf);
    break;
  case '\b':
    fputs("\\b", outf);
    break;
  case '\n':
    fputs("\\n", outf);
    break;
  case '\t':
    fputs("\\t", outf);
    break;
  // and so on
  default:
    fputc(ch, outf);
}

这篇关于如何将C字符串转换成在C逃脱它的版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆