Table of Contents

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

bytes byte[]

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

bytes byte[]

The byte[] to extend.

Returns

string

A binary string representation of the elements in bytes.

Exceptions

ArgumentNullException

bytes is 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

bytes byte[]

The byte[] to extend.

setup Action<EncodingOptions>

The EncodingOptions which need to be configured.

Returns

string

A string containing the results of decoding the specified sequence of bytes.

Remarks

ToHexadecimalString(byte[])

Converts the specified bytes to its equivalent hexadecimal representation.

public static string ToHexadecimalString(this byte[] bytes)

Parameters

bytes byte[]

The byte[] to extend.

Returns

string

A hexadecimal string representation of the elements in bytes.

Exceptions

ArgumentNullException

bytes is 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

bytes byte[]

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)

Tries to resolve the Unicode Encoding object from the specified byte array.

public static bool TryDetectUnicodeEncoding(this byte[] bytes, out Encoding result)

Parameters

bytes byte[]

The byte[] to extend.

result Encoding

When 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 the bytes parameter is null, or does not contain a Unicode representation of an Encoding.

Returns

bool

true if the bytes parameter was converted successfully; otherwise, false.