Skip to content

Instantly share code, notes, and snippets.

@nenono
Created October 4, 2019 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nenono/fa14f79daae5ba8e6a46f35c8a543303 to your computer and use it in GitHub Desktop.
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.
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();
}
}
}
}
@nagayoshino
Copy link

using System.Text; とかも必要だけど察して

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment