Created
October 4, 2019 10:46
-
-
Save nenono/fa14f79daae5ba8e6a46f35c8a543303 to your computer and use it in GitHub Desktop.
(.NET)How to convert string to bytes array that is encoded UTF-8 with BOM.
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
using System.IO; | |
namespace Sample | |
{ | |
public static class EncodingExtension | |
{ | |
public static byte[] GetUtf8WithBomBytes(this string str) | |
{ | |
var encoding = new UTF8Encoding(true) | |
using (var stream = new MemoryStream()) | |
using (var writer = new StreamWriter(stream, encoding)) | |
{ | |
writer.Write(str); | |
var bytes = stream.ToArray(); | |
return encoding.GetPreamble().Concat(bytes).ToArray(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
using System.Text; とかも必要だけど察して