Class StringFactory
- Namespace
- Cuemon
- Assembly
- Cuemon.Core.dll
Provides access to factory methods for creating encoded string representations.
public static class StringFactory
- Inheritance
-
StringFactory
Examples
The following example demonstrates how to use StringFactory to generate hexadecimal, binary, URL-safe Base64, protocol-relative URL, and URI scheme strings from .NET data types.
using System;
namespace Cuemon;
public class StringFactoryExample
{
public void Demonstrate()
{
// Convert bytes to hexadecimal string
byte[] binaryData = { 0x0F, 0xA0, 0x01 };
string hex = StringFactory.CreateHexadecimal(binaryData);
Console.WriteLine(hex); // 0fa001
// Convert string to hexadecimal representation
string hexFromString = StringFactory.CreateHexadecimal("Hello");
Console.WriteLine(hexFromString); // 48656c6c6f
// Create binary digit string from bytes
string binary = StringFactory.CreateBinaryDigits(new byte[] { 0, 1, 255 });
Console.WriteLine(binary); // 000000000000000111111111
// Create URL-safe Base64 string
string urlSafe = StringFactory.CreateUrlEncodedBase64(new byte[] { 251, 255 });
Console.WriteLine(urlSafe); // -_8
// Create a protocol-relative URL (// prefix replaces https://)
string relativeUrl = StringFactory.CreateProtocolRelativeUrl(
new Uri("https://www.cuemon.net/about"));
Console.WriteLine(relativeUrl); // //www.cuemon.net/about
// Get string representation of a URI scheme enum
string scheme = StringFactory.CreateUriScheme(UriScheme.Https);
Console.WriteLine(scheme); // https
}
}
Methods
CreateBinaryDigits(byte[])
Creates a binary digit string representation of the specified byte array.
public static string CreateBinaryDigits(byte[] value)
Parameters
valuebyte[]The byte array to convert.
Returns
- string
A binary digit string representation of
value.
Exceptions
- ArgumentNullException
valueis null.
CreateHexadecimal(byte[])
Creates a hexadecimal string representation of the specified byte array.
public static string CreateHexadecimal(byte[] value)
Parameters
valuebyte[]The byte array to convert.
Returns
- string
A hexadecimal string representation of
value.
Exceptions
- ArgumentNullException
valueis null.
CreateHexadecimal(string, Action<EncodingOptions>)
Creates a hexadecimal string representation of the specified string.
public static string CreateHexadecimal(string value, Action<EncodingOptions> setup = null)
Parameters
valuestringThe string to convert.
setupAction<EncodingOptions>The delegate that configures the encoding behavior.
Returns
- string
A hexadecimal string representation of
value.
Exceptions
- ArgumentNullException
valueis null.- InvalidEnumArgumentException
setupconfigures an invalid value for Preamble.
CreateProtocolRelativeUrl(Uri, Action<ProtocolRelativeUriStringOptions>)
Creates a protocol-relative URL string representation of the specified Uri.
public static string CreateProtocolRelativeUrl(Uri value, Action<ProtocolRelativeUriStringOptions> setup = null)
Parameters
valueUriThe URI to convert.
setupAction<ProtocolRelativeUriStringOptions>The delegate that configures the protocol-relative URL format.
Returns
- string
A protocol-relative URL string representation of
value.
Exceptions
- ArgumentNullException
valueis null.- ArgumentException
valueis not an absolute URI.
CreateUriScheme(UriScheme)
Creates the string representation of the specified UriScheme.
public static string CreateUriScheme(UriScheme value)
Parameters
valueUriSchemeThe URI scheme to convert.
Returns
- string
The string representation of
value.
Remarks
Returns the string representation of Undefined when value
is not found in the lookup table.
CreateUrlEncodedBase64(byte[])
Creates a URL-safe Base64 string representation of the specified byte array.
public static string CreateUrlEncodedBase64(byte[] value)
Parameters
valuebyte[]The byte array to convert.
Returns
- string
A URL-safe Base64 string representation of
value.
Remarks
This method uses the Base64 URL encoding convention by removing padding characters and replacing
+ with - and / with _.
The implementation was inspired by Appendix C of the JSON Web Signature (JWS) draft specification.
Exceptions
- ArgumentNullException
valueis null.