[英]string “7\aAZEA\0\a\rPV” sent to a WCF service is received as “7\aAZEA\0\a\nPV”
When I send the string 当我发送字符串时
"7\\aAZEA\\0\\a\\rPV"
“ 7 \\ aAZEA \\ 0 \\ a \\ rPV”
to a WCF service it's received as 到WCF服务,它作为
"7\\aAZEA\\0\\a\\nPV"
“ 7 \\ aAZEA \\ 0 \\ a \\ nPV”
ie the \\r is changed to \\n . 即\\ r更改为\\ n 。
I assume this is due to " /r " being a control character. 我认为这是由于“ / r ”是控制字符。
This is an encrypted string so I can't manually replace characters. 这是一个加密的字符串,所以我不能手动替换字符。
How can I prevent this? 我该如何预防?
Thanks 谢谢
The comments already helped, but hope this will clarify the reasoning behind it. 这些评论已经有所帮助,但希望这将阐明其背后的原因。
In a string a backslash is always used for control characters, \\r
being carriage return, \\n
being a line feed and \\0
being a null character. 在字符串中,反斜杠始终用于控制字符,
\\r
为回车符, \\n
为换行符, \\0
为空字符。 You are indeed correct, \\r
is being interpreted as a carriage return (which it replaces with the \\n
line feed). 您确实是正确的,
\\r
被解释为回车符(将其替换为\\n
换行符)。 Because \\a
does not mean anything, it is sent as two characters ( \\
and a
). 因为
\\a
没有任何意义,所以它以两个字符( \\
和a
)发送。 To make sure a backslash is interpreted as a backslash in a string (and not possibly as a control character), a double backslash \\\\
is used. 为了确保将反斜杠解释为字符串中的反斜杠(而不是控制字符),请使用双反斜杠
\\\\
。
When you require to send other strings, make sure that you use double backslashes everywhere the string actually requires a backslash and the following character to be sent. 当您需要发送其他字符串时,请确保在字符串实际需要反斜杠和要发送的以下字符的所有地方都使用双反斜杠。
In your string \\0
is interpreted as a single NULL character, not the two characters \\
and 0
. 您的字符串
\\0
被解释为单个NULL字符,而不是两个字符\\
和0
。 This could be what you want, but if not, it's best to write \\\\0
. 这可能是您想要的,但是如果不是,最好写
\\\\0
。
To conclude, if you want to send your string literally, this is what you should send: 总而言之,如果您想直接发送字符串,则应发送以下内容:
"7\\aAZEA\\0\\a\\nPV"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.