0

I'm currently trying to add this prefix to a TCP IP Message I'm sending

\r\x1c\r

But when the receiver gets it they see

\\r\\x1c\\r

I tried

"\\r\\x1c\\r" + message
@"\r\x1c\r" + message

But I keep getting this in front of the message:

\\r\\x1c\\r
MindGame
  • 1,211
  • 6
  • 29
  • 50
  • 3
    You are probably viewing this in the debugger because if you print that out to something like the Console or a file it will be what you expect it to be. If you still think it is an error you need to create an [mcve]. You can use https://dotnetfiddle.net/ and post the link in your question. – Igor Mar 08 '19 at 20:03
  • Yeah. I thought that also. But the receiver of the TCP IP message sees the double slashes. – MindGame Mar 08 '19 at 20:05
  • 1
    If you want help you will need to provide a [mcve]. – Igor Mar 08 '19 at 20:06
  • Where are you seeing that? How? Through another debugger visualizer? –  Mar 08 '19 at 20:06
  • I see it in the debugger in the watch tab. When I actually do a preview of the string the escape characters are not there. – MindGame Mar 08 '19 at 20:08
  • 3
    Are you really sure that's what it is seeing? Lots of tools mimic the debugger's behavior (showing backslashes as doubles). That "backslash is the escape character" behavior is common to most C and C-derived languages. Have you, for instance, checked the string length on the other side? – Flydog57 Mar 08 '19 at 20:08
  • 2
    That is expected behavior, that is how the watch window works. – Igor Mar 08 '19 at 20:08
  • Possible duplicate of [Stop visual studio debug putting slash in string containing double quotes](https://stackoverflow.com/q/41172620/1260204) – Igor Mar 08 '19 at 20:10
  • See if this helps: https://stackoverflow.com/a/17918150/2348125 – Amir Molaei Mar 08 '19 at 20:10
  • Possible duplicate of [Escape Characters Aren't Working In Debugger (C#)](https://stackoverflow.com/questions/31410576/escape-characters-arent-working-in-debugger-c) – Igor Mar 08 '19 at 20:10
  • So the customer keeps getting the double slashes. He went on to tell me this \r\x1c\r ==> 5c 72 5c 78 31 63 5c 72. Does that help any? – MindGame Mar 08 '19 at 20:10
  • 1
    That doesn't look like double slashes to me. Double slashes would be 5c 5c 72 etc. "5c 72" is just "\r". No doubling of the slash. – Jon Skeet Mar 08 '19 at 20:17
  • @JonSkeet So you are suggesting to just do this "\r\x1c\r" + message ? – MindGame Mar 08 '19 at 20:20
  • 2
    I'm suggesting that your customer is not getting double slashes. I don't know what bytes you *intend* them to receive, as we don't know what sort of protocol you're talking. But you should focus on the bytes that you expect to send, and the bytes you're actually sending. Ignore debugger string representations, which can be highly misleading. – Jon Skeet Mar 08 '19 at 20:24
  • @JonSkeet Thanks for the advice on focusing on what I'm sending. Yes the debugger is very misleading and sent me down the wrong path. – MindGame Mar 08 '19 at 22:33

1 Answers1

3

It seems like you're trying to actually transfer \r and \x1c as control characters instead. Therefore, you should not escape the backslash.

Send the message like "\r\x1c\r" + message or $"\r\x1c\r{message}" (without @).

When you're using "\\r" or @"\r", you're escaping the backslash character and the message will be sent as textual \r. When you check the value at the receiver end, using a debugger or watch-window, it will show up as "\\r".

huysentruitw
  • 27,376
  • 9
  • 90
  • 133
  • Yes you are right. Its like telling them its the beginning of the message. Let me try it out. Thanks! – MindGame Mar 08 '19 at 20:16