Hexalith.Commons.StringEncoders 1.66.5

dotnet add package Hexalith.Commons.StringEncoders --version 1.66.5
                    
NuGet\Install-Package Hexalith.Commons.StringEncoders -Version 1.66.5
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Hexalith.Commons.StringEncoders" Version="1.66.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Hexalith.Commons.StringEncoders" Version="1.66.5" />
                    
Directory.Packages.props
<PackageReference Include="Hexalith.Commons.StringEncoders" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Hexalith.Commons.StringEncoders --version 1.66.5
                    
#r "nuget: Hexalith.Commons.StringEncoders, 1.66.5"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=Hexalith.Commons.StringEncoders&version=1.66.5
                    
Install Hexalith.Commons.StringEncoders as a Cake Addin
#tool nuget:?package=Hexalith.Commons.StringEncoders&version=1.66.5
                    
Install Hexalith.Commons.StringEncoders as a Cake Tool

Hexalith.Commons.StringEncoders

Overview

Hexalith.Commons.StringEncoders is a .NET library that provides utilities for encoding and decoding strings to be compliant with RFC 1123 format. This library ensures that strings can be used in contexts with restricted character sets while preserving the original data for later reversal.

The main purpose of this library is to provide a reversible mechanism for encoding arbitrary strings using a restricted character set often associated with RFC 1123 contexts (like headers or identifiers).

Features

  • Encode strings to be RFC 1123 compliant (using only A-Z, a-z, 0-9, '-', '.')
  • Decode RFC 1123 compliant strings back to their original form
  • Lossless round-trip encoding/decoding for any string, including those with special characters or Unicode
  • Proper handling of UTF-8 encoding for international characters

Installation

Package Manager Console

Install-Package Hexalith.Commons.StringEncoders

.NET CLI

dotnet add package Hexalith.Commons.StringEncoders

Usage

Basic Usage

using Hexalith.Commons.StringEncoders;

// Encoding a string to be RFC 1123 compliant
string original = "Hello World! 你好世界";
string encoded = original.ToRFC1123();
// Result: "Hello_20World_21_20_E4_BD_A0_E5_A5_BD_E4_B8_96_E7_95_8C"

// Decoding back to the original string
string decoded = encoded.FromRFC1123();
// Result: "Hello World! 你好世界"

Handling Special Characters

The library automatically escapes characters that are not in the allowed set:

  • Spaces become _20
  • Underscores are escaped as __ (double underscore)
  • Other special characters and Unicode are escaped as _XX where XX is the hex representation of the UTF-8 byte
string withSpecialChars = "user@example.com";
string encoded = withSpecialChars.ToRFC1123();
// Result: "user_40example.com"

string decoded = encoded.FromRFC1123();
// Result: "user@example.com"

API Reference

ToRFC1123

public static string ToRFC1123(this string input)

Converts a string into a representation where characters not in the allowed set (A-Z, a-z, 0-9, '-', '.') are escaped using a reversible mechanism (_XX for UTF-8 bytes). The escape character itself (_) is escaped as __.

Parameters:

  • input: The string to convert.

Returns:

  • The escaped string, or the original string if null or empty.

FromRFC1123

public static string FromRFC1123(this string input)

Converts a string previously escaped using ToRFC1123 back to its original form. It reverses the _XX (UTF-8 byte hex) and __ (literal underscore) escaping.

Parameters:

  • input: The escaped string.

Returns:

  • The original, decoded string, or the original string if null or empty.

Exceptions:

  • FormatException: Thrown if the input string contains invalid escape sequences.

Use Cases

  • Creating file names or paths from arbitrary strings
  • Generating URL-safe identifiers
  • Storing data in systems with character restrictions
  • Creating headers or identifiers that need to be RFC 1123 compliant

License

This project is licensed under the MIT License - see the LICENSE file in the project root for details.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net9.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Hexalith.Commons.StringEncoders:

Package Downloads
Hexalith.KeyValueStorages.DaprComponents

Hexalith KeyValueStorages utilities and helpers

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.66.5 103 4/11/2025
1.66.4 157 4/10/2025
1.66.3 142 4/9/2025
1.66.2 155 4/8/2025
1.66.1 140 4/8/2025
1.66.0 144 4/7/2025
1.65.1 265 4/6/2025
1.65.0 151 4/6/2025
1.0.0 151 4/6/2025