ByteOrderMark Class
Definition
Provides static helper methods for detecting, decoding, and removing Unicode byte order marks (BOMs).
public static class ByteOrderMark
- Inheritance
-
ByteOrderMark
Examples
The following example demonstrates how to detect and remove byte order marks (BOM) from a UTF-8 byte array and detect encoding from a stream. The output shows the detected encoding name, the byte count after BOM removal, and the encoding detected from the stream.
using System;
using System.IO;
using System.Text;
using Cuemon.Text;
namespace Cuemon.Text;
public class ByteOrderMarkExample
{
public void Demonstrate()
{
var utf8Bytes = new byte[] { 0xEF, 0xBB, 0xBF, 0x48, 0x65, 0x6C, 0x6C, 0x6F };
var encoding = ByteOrderMark.Decode(utf8Bytes);
Console.WriteLine($"Detected encoding: {encoding.EncodingName}");
var bytes = ByteOrderMark.Remove(utf8Bytes, Encoding.UTF8);
Console.WriteLine($"BOM removed, remaining length: {bytes.Length}");
using var stream = new MemoryStream(utf8Bytes);
if (ByteOrderMark.TryDetectEncoding(stream, out var detected))
{
Console.WriteLine($"Stream encoding: {detected.EncodingName}");
}
}
}
Methods
| Name | Description |
|---|---|
| Decode(byte[]) | Decodes the byte order mark (BOM) in the specified byte array to its corresponding Encoding. |
| DetectEncodingOrDefault(byte[], Encoding) | Detects the encoding of the specified byte array, or returns a fallback encoding if detection fails. |
| DetectEncodingOrDefault(Stream, Encoding) | Detects the encoding of the specified stream, or returns a fallback encoding if detection fails. |
| Remove(byte[], Encoding) | Removes the preamble, if present, from the specified byte array. |
| Remove(Stream, Encoding, Action<DisposableOptions>) | Removes the preamble, if present, from the specified stream. |
| TryDetectEncoding(byte[], out Encoding) | Tries to detect the encoding represented by the byte order mark in the specified byte array. |
| TryDetectEncoding(Stream, out Encoding) | Tries to detect the encoding represented by the byte order mark in the specified stream. |