How to convert a string to bytes with the end of a unix string?

If I convert the string to bytes using:

byte[] ascii = new System.Text.ASCIIEncoding().GetBytes(myString);

The returned bytes then use the Windows line endings. How can I convert it using Unix line endings?

+3
source share
2 answers

Line endings are independent of encoding. If you want to convert the ending of Windows lines to the end of a Unix line, do this on the line itself:

myString = myString.Replace("\r\n", "\n");

Personally, I avoid using ASCII where possible, by the way - are you absolutely sure that he will never need any accented characters? If I get a choice, I usually use UTF-8:

myString = myString.Replace("\r\n", "\n");
byte[] bytes = Encoding.UTF8.GetBytes(myString);

- , StreamWriter File.CreateText .. , .

+9
string myString = @"line 1
line 2";
byte[] ascii = new System.Text.ASCIIEncoding().GetBytes(myString.Replace(Environment.NewLine, "\n"));
+2

Source: https://habr.com/ru/post/1791959/


All Articles