HexEncoding

Namespace: Babel.Licensing Assembly: Babel.Licensing.dll

Converts between binary data and an hex-encoded string. This class cannot be inherited.

public sealed class HexEncoding : IEncoding

Inheritance

objectHexEncoding

Implements

IEncoding

Inherited Members

object.GetType(), object.ToString(), object.Equals(object?), object.Equals(object?, object?), object.ReferenceEquals(object?, object?), object.GetHashCode()

Constructors

HexEncoding()

public HexEncoding()

Methods

Decode(string)

Decodes the given hex string to its byte representation.

public byte[] Decode(string encoded)

Parameters

NameDescription

encoded string

The encoded string.

Returns

NameDescription

The decoded string buffer.

Examples

// Encode and decoding using HexEncoding
HexEncoding hex = new HexEncoding();
byte[] data = Enumerable.Range(1, 10).Select(i => (byte)i).ToArray();

// Encode data byte[]
// 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xA
string encoded = hex.Encode(data);

// encoded string
// 0102030405060708090A

// Decode encodes string 
byte[] decoded = hex.Decode(encoded);

// decoded byte[]
// 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xA

Exceptions

NameDescription

Thrown when one or more required arguments are null.

Encode(byte[])

Encodes the given buffer to an hex string.

public string Encode(byte[] buffer)

Parameters

NameDescription

buffer byte[]

The buffer.

Returns

NameDescription

The encoded hex string.

Examples

// Encode and decoding using HexEncoding
HexEncoding hex = new HexEncoding();
byte[] data = Enumerable.Range(1, 10).Select(i => (byte)i).ToArray();

// Encode data byte[]
// 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xA
string encoded = hex.Encode(data);

// encoded string
// 0102030405060708090A

// Decode encodes string 
byte[] decoded = hex.Decode(encoded);

// decoded byte[]
// 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xA

Exceptions

NameDescription

Thrown when one or more required arguments are null.

IsValidChar(char)

Query if the given character is a valid hex character.

public bool IsValidChar(char value)

Parameters

NameDescription

value char

The input character.

Returns

NameDescription

true if valid character, false if not.

TryDecode(string, out byte[])

Attempts to decode from the given data.

public static bool TryDecode(string encoded, out byte[] buffer)

Parameters

NameDescription

encoded string

The encoded string.

buffer byte[]

[out] The buffer.

Returns

NameDescription

true if it succeeds, false if it fails.

Last updated