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
object ← HexEncoding
Implements
IEncoding
Inherited Members
object.GetType(), object.ToString(), object.Equals(object?), object.Equals(object?, object?), object.ReferenceEquals(object?, object?), object.GetHashCode()
Constructors
HexEncoding()
Methods
Decode(string)
Decodes the given hex string to its byte representation.
public byte[] Decode(string encoded)
Parameters
Returns
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
' Encode and decoding using HexEncoding
Dim hex As New HexEncoding()
Dim data As Byte() = Enumerable.Range(1, 10).[Select](Function(i) CByte(i)).ToArray()
' Encode data byte[]
' 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xA
Dim encoded As String = hex.Encode(data)
' encoded string
' 0102030405060708090A
' Decode encodes string
Dim decoded As Byte() = hex.Decode(encoded)
' decoded byte[]
' 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xA
Exceptions
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
Returns
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
' Encode and decoding using HexEncoding
Dim hex As New HexEncoding()
Dim data As Byte() = Enumerable.Range(1, 10).[Select](Function(i) CByte(i)).ToArray()
' Encode data byte[]
' 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xA
Dim encoded As String = hex.Encode(data)
' encoded string
' 0102030405060708090A
' Decode encodes string
Dim decoded As Byte() = hex.Decode(encoded)
' decoded byte[]
' 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xA
Exceptions
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
Returns
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
Returns
true if it succeeds, false if it fails.