0

I have a function that received a string ex. "rn" and I need to append the '\' character to each letter of the string to get "\r\n". In order to append the '\' character, I need to escape it with an additional '\', however that gives me the wrong string.

For example:

QString s = "rn";
QString n = QString("\\%1\\%2").arg(s[0]).arg(s[1]);
qDebug() << n.compare("\r\n");

Outputs 79 instead of 0. I need the first string (s) to be identical to "\r\n" but I'm not sure how I can properly append the '\' character.

ti7
  • 16,375
  • 6
  • 40
  • 68
programmer
  • 27
  • 4
  • n is `"\\r\\n"` instead of `"\r\n"` – Cwift Aug 26 '21 at 03:00
  • How would I make it just \r\n though? If I try QString("\%1\%2) the compare output is 101 – programmer Aug 26 '21 at 03:03
  • `\r` is a escape character, it is completely different from the character n. I'd like use a switch-case – Cwift Aug 26 '21 at 03:11
  • Your question is contradictory. (First of all, your example shows "prepend" instead of "append".) If you prepend one character to another, you get two characters. However, `\r` is one character, even though we describe it with two characters. Which is your real goal, prepending or being identical to `"\r\n"`? You should make it clear in your question which is the goal and which is your failed attempt. – JaMiT Aug 26 '21 at 03:19
  • Assuming you actually want to transform the letter `r` to the carriage-return character, [Convert string with explicit escape sequence into relative character](https://stackoverflow.com/questions/5612182/) is related, but you can do better than prepending a backslash then converting the newly-made escape sequence. (That is, the overall strategy can be used, but details of the implementation will differ.) – JaMiT Aug 26 '21 at 03:24

2 Answers2

1

The way you want does not work, since the escape sequences are interpreted by the compiler, not in runtime.

The quite easy solution is using a function producing required special chars:

char get_special_char(char ch) {
  switch (ch) {
    case 'n':
      return '\n';
    case 'r':
      return '\r';
    default:
     return ch;
  }
}

QString n = QString("%1%2").arg(get_special_char(s[0]))
                           .arg(get_special_char(s[1]));

If there is a lot of chars for conversion to the special symbols, you can use an array char[256] filled with required symbols: a['n'] -> '\n', a['r'] -> '\r'. Be careful with the signed char type.

273K
  • 29,503
  • 10
  • 41
  • 64
0

You can write "\\" for each '\', typecast int 92 to char, or append '\\' (already a char)

This is necessary because \ is the escape character for strings in C-like and most other languages, and so leads to trouble between how it's displayed and how it's represented

To convince yourself it's the same, you can display / in other ways, such as binary or hex

#include <stdio.h>

int main()
{
  printf("%c\n", '\\');
  printf("%04x\n", '\\');  // display hex
  printf("%04x\n", "\\"[0]);
  printf("%04x\n", 92);
  return 0;
}
% g++ test.py && ./a.out
\
005c
005c
005c
ti7
  • 16,375
  • 6
  • 40
  • 68