Created
August 14, 2018 13:49
-
-
Save milovidov983/e6bd83028d694cc0c2c8d647b94a99d1 to your computer and use it in GitHub Desktop.
How to convert UTF-8 to UTF-8 with BOM c# string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private string ConvertStringToUtf8Bom(string source) { | |
| var data = Encoding.UTF8.GetBytes(source); | |
| var result = Encoding.UTF8.GetPreamble().Concat(data).ToArray(); | |
| var encoder = new UTF8Encoding(true); | |
| return encoder.GetString(result); | |
| } |
By the way, I am using stream writer to create a new file with Encoding.UTF8, and it will handle the BOM automatically.
https://github.com/dotnet/runtime/blob/6ef4b2e7aba70c514d85c2b43eac1616216bea55/src/libraries/System.Private.CoreLib/src/System/IO/StreamWriter.cs#L273
does the above code add Carriage return when processing ?
Thanks guys! This helped!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@milovidov983,Thanks, I tried to print the bytes array, it make sense.
False
length of aîn is 3
61 C3 AE 6E
length of aîn is 4
EF BB BF 61 C3 AE 6E