Skip to content

Instantly share code, notes, and snippets.

@jstclair
Created November 19, 2010 19:35
Show Gist options
  • Save jstclair/707017 to your computer and use it in GitHub Desktop.
Save jstclair/707017 to your computer and use it in GitHub Desktop.
How to get a UTF8Encoder with a BOM
using System;
using System.Linq;
using System.Text;
namespace BomEncoding
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("with BOM:");
WriteUtf(new UTF8Encoding(true));
Console.WriteLine("without BOM:");
WriteUtf(new UTF8Encoding(false));
Console.ReadLine();
}
private static void WriteUtf(UTF8Encoding encoder)
{
var preamble = encoder.GetPreamble();
preamble.ToList().ForEach(x => Console.Write("[{0}] ", x));
Console.WriteLine();
var text = encoder.GetBytes("Some Text");
text.ToList().ForEach(x => Console.Write("[{0}] ", x));
Console.WriteLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment