0

I have the following string :

This is the string to
test carriage return
using c#

The above string has two carriage returns in the end of lines. I have to encode this string to pass to a WCF service and then decode the string for further process.

//TO ENCODE
            byte[] bytes = Encoding.Default.GetBytes(rawstring);
            string encodedStr = Encoding.UTF8.GetString(bytes);


//TO DECODE
            byte[] rawUtf8EncodedData = Encoding.Default.GetBytes(encodedstring);
            string decodedStr =  Encoding.UTF8.GetString(rawUtf8EncodedData);

After Encoding I get the below string :

This is the string to\ntest carriage return\nusing c#

After Decoding I get the below string :

This is the string to\ntest carriage return\nusing c#

Problem is, I need the carriage return back for some reason and not the \n in my string after decoding. How can I achieve this ? The above string is only for illustration and in actual string, there would be lots of carriage returns and symbols

mjwills
  • 23,389
  • 6
  • 40
  • 63
Anuya
  • 8,082
  • 49
  • 137
  • 222
  • If you are viewing the strings in the debugger, it will show you `\n` instead of a carriage return. Try outputting the string to the console or a log file and see if the carriage returns are there. – D Stanley Nov 27 '18 at 02:03
  • I am using this string to do some logic, which will fail if \n is there. It requires a carriage return to work. And right now it fails, because \n is present in the raw string. – Anuya Nov 27 '18 at 02:07
  • Where do you *"have the following string"*? Line ends can be encoded in a variety of ways – Flydog57 Nov 27 '18 at 02:34
  • Just tested (to be sure). Inserting `\r\n` or `\r` or `\n`, all are returned as the original. What do you mean with *[the] string has two carriage returns*? Are you extracting this string from a `RichTextBox`, maybe?. – Jimi Nov 27 '18 at 02:35
  • @DStanley `\n` is not a carriage return. It is a line feed. `\r` is a carriage return. – mjwills Nov 27 '18 at 02:49
  • `The above string has two carriage returns in the end of lines` The short answer is that your original string does **not** have a carriage return in it. Where is the original string coming from? Is it a C# program? Somewhere else? What OS is it coming from? – mjwills Nov 27 '18 at 02:51
  • 1
    Possible duplicate of [What is a quick way to force CRLF in C# / .NET?](https://stackoverflow.com/questions/841396/what-is-a-quick-way-to-force-crlf-in-c-sharp-net) – mjwills Nov 27 '18 at 02:52
  • What .Net is this running on? – Joel Coehoorn Nov 27 '18 at 03:26

2 Answers2

3

This is wrong:

byte[] bytes = Encoding.Default.GetBytes(rawstring);
string encodedStr = Encoding.UTF8.GetString(bytes);

The first line gets bytes using the Default encoding. On .Net Core it's UTF-8 and you're likely okay, but on Windows it's the system code page from the OS, which could be anything.

The second line of code then treats those bytes as if they are already in UTF-8 format, regardless of you what you actually got. This might work for a lot of strings, but eventually it's gonna fail spectacularly. Morever, the result is still a .Net string, which is the Unicode (UTF-16) encoding internally... so what you end up with is still UTF-16, not UTF-8.

If you need to send a UTF-8 string to a network service, get the UTF-8 bytes using Encoding.UTF8.GetBytes(), and send the bytes as if they were a string.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • Definitely. This *conversion* will not fail for a very limited sub-set of characters. Depending on the Local Encoding, possibly none. Outside these limited CodePoints, you get *garbage*. – Jimi Nov 27 '18 at 03:28
-1

Replacing \n with \r\n works fine

Anuya
  • 8,082
  • 49
  • 137
  • 222