Class ByteExtensions
- Namespace
- Cuemon.Extensions
- Assembly
- Cuemon.Extensions.Core.dll
Extension methods for the byte struct.
public static class ByteExtensions
- Inheritance
-
ByteExtensions
Examples
The following example demonstrates how to use the ByteExtensions extension methods to convert byte arrays to various string representations and detect Unicode encoding.
using System;
using System.Text;
using Cuemon.Extensions;
namespace MyApp.Examples;
public static class ByteExtensionsExample
{
public static void Demonstrate()
{
byte[] data = { 0xEF, 0xBB, 0xBF, 0x48, 0x65, 0x6C, 0x6C, 0x6F };
string text = data.ToEncodedString();
string hex = data.ToHexadecimalString();
string binary = data.ToBinaryString();
string base64 = data.ToBase64String();
string urlBase64 = data.ToUrlEncodedBase64String();
data.TryDetectUnicodeEncoding(out Encoding detectedEncoding);
Console.WriteLine(text);
Console.WriteLine(hex);
Console.WriteLine(binary);
Console.WriteLine(base64);
Console.WriteLine(urlBase64);
Console.WriteLine(detectedEncoding?.WebName ?? "unknown");
}
}
Methods
ToBase64String(byte[])
Converts an array of 8-bit unsigned integers to its equivalent string representation that is encoded with base-64 digits.
public static string ToBase64String(this byte[] bytes)
Parameters
bytesbyte[]The byte[] to extend.
Returns
- string
The string representation, in base 64, of the contents of
bytes.
ToBinaryString(byte[])
Converts the specified bytes to its equivalent binary representation.
public static string ToBinaryString(this byte[] bytes)
Parameters
bytesbyte[]The byte[] to extend.
Returns
Exceptions
- ArgumentNullException
bytesis null.
ToEncodedString(byte[], Action<EncodingOptions>)
Converts the specified bytes to a string using the provided preferred encoding.
public static string ToEncodedString(this byte[] bytes, Action<EncodingOptions> setup = null)
Parameters
bytesbyte[]The byte[] to extend.
setupAction<EncodingOptions>The EncodingOptions which need to be configured.
Returns
Remarks
EncodingOptions will be initialized with DefaultPreambleSequence and DefaultEncoding.
ToHexadecimalString(byte[])
Converts the specified bytes to its equivalent hexadecimal representation.
public static string ToHexadecimalString(this byte[] bytes)
Parameters
bytesbyte[]The byte[] to extend.
Returns
Exceptions
- ArgumentNullException
bytesis null.
ToUrlEncodedBase64String(byte[])
Encodes a byte array into its equivalent string representation using base 64 digits, which is usable for transmission on the URL.
public static string ToUrlEncodedBase64String(this byte[] bytes)
Parameters
bytesbyte[]The byte[] to extend.
Returns
- string
The string containing the encoded token if the byte array length is greater than one; otherwise, an empty string ("").
TryDetectUnicodeEncoding(byte[], out Encoding)
public static bool TryDetectUnicodeEncoding(this byte[] bytes, out Encoding result)
Parameters
bytesbyte[]The byte[] to extend.
resultEncodingWhen this method returns, it contains the Unicode Encoding value equivalent to the encoding contained in
bytes, if the conversion succeeded, or a null reference (Nothing in Visual Basic) if the conversion failed. The conversion fails if thebytesparameter is null, or does not contain a Unicode representation of an Encoding.
Returns
- bool
trueif thebytesparameter was converted successfully; otherwise,false.