How do I convert a string to bytes with unix line endings?


If I convert a string to bytes using:

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

Then the bytes I get back use Windows line endings. How can I convert it using Unix line endings?


The line endings are independent of the encoding. If you want to convert Windows line endings to Unix line endings, do that in the string itself:

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

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

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

If you're writing to a stream somewhere, any alternative is to use StreamWriter, or File.CreateText etc. Then you specify the encoding once but don't need to deal with the raw bytes yourself.


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


  • Does Azure Cosmos DB not support MongoDB/Mongoose unique index?
  • UIKit/UIKit.h file not found - XCode 4.6.1 - Objective-C
  • Show GeoTiff image Openlayers 3
  • Show Message dialog while executing
  • Get locale (language) of website visitor? [duplicate]
  • Generating tree from database in Yii
  • How to make a marker appear or disappear based on zoom level on Google Maps v2
  • Repeated String Match
  • How to update the learning rate in a two layered multi-layered perceptron?
  • Changing a graph to list of nodes in Haskell
  • Does Tomcat cache compiled JSP pages in memory if scratchdir is read-only?
  • twitter bootstrap for high resolution mobile displays
  • isotope shakes after the transition
  • How to run python3.7 based flask web api on azure
  • SCNText Not Displaying
  • Create a log file for a custom DNN module
  • Project Euler -Prob. #20 (Lua)
  • how to reload page after uploading images
  • MySQL ordering a date range to the beginning, then ordering everything else by other criteria
  • Using Java runtime to add registry key, cause process reg.exe to run forever
  • Spring annotation @Order
  • Was there ever a proposal to include the URL fragment into the HTTP request?
  • How to put an object in the air?
  • Please update your Node runtime to version >=0.12.x
  • Sample deviceQuery cuda program
  • Android studio import problems. (Apktool)
  • Admob in ListView not clickable
  • Connect to a local database from phpmyadmin with R
  • Building JavaFX 2.0 App on Mac, deploying on Windows
  • Create an Office365 mailbox from within C# Web API method
  • JavaMail connection problems [duplicate]
  • Getting the type of a “Type” in C# reflection
  • Creating 2d platforms using JavaScript
  • How to define something in JavaScript [closed]
  • Using redis as an LRU cache for postgres
  • Grails - How to implement a foreign key relationship not using an id column?
  • How to use FirstOrDefault inside Include