BaseAnyEncoding 1.0.1

dotnet add package BaseAnyEncoding --version 1.0.1                
NuGet\Install-Package BaseAnyEncoding -Version 1.0.1                
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="BaseAnyEncoding" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add BaseAnyEncoding --version 1.0.1                
#r "nuget: BaseAnyEncoding, 1.0.1"                
#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.
// Install BaseAnyEncoding as a Cake Addin
#addin nuget:?package=BaseAnyEncoding&version=1.0.1

// Install BaseAnyEncoding as a Cake Tool
#tool nuget:?package=BaseAnyEncoding&version=1.0.1                

BaseAnyEncoding

Data encoding library, capable of character set encoding (base16 base32 base58 base62, etc.), or any type of custom encoding. 数据编码库,可进行字符集编码(base16 base32 base58 base62等),或任意类型的自定义编码。

Note

  • 由于使用通用编码代码,所以效率会低于特化编码代码;
  • 编码基础数据大小不能小于2;
  • 编码基础数据越大编码后的输出越小;
  • 使用 BaseXX 的字符集即可进行对应的编码,但:
    • Base64 不会填充末尾 =;
    • 与标准 Base85 不兼容;
    • 未测试所有 BaseXX 与其标准编码的结果兼容性;

快速开始

引用包

<ItemGroup>
  <PackageReference Include="BaseAnyEncoding" Version="1.0.0" />
</ItemGroup>

静态方法调用

使用 System.Buffers.BaseAnyEncoding.Encode 进行编码,System.Buffers.BaseAnyEncoding.Decode 进行解码

  • 使用任意大小字符集(*)作为编码基础

    var charset = "嗷呜!".AsSpan();
    var source = Encoding.UTF8.GetBytes("hello world");
    
    using var encodedData = System.Buffers.BaseAnyEncoding.Encode(source, charset);
    Console.WriteLine("EncodedData: " + encodedData.Span.ToString());
    
    using var decodedData = System.Buffers.BaseAnyEncoding.Decode(encodedData.Span, charset);
    Console.WriteLine("DecodedData: " + Encoding.UTF8.GetString(decodedData.Span));
    

    输出为:

    EncodedData: !嗷呜呜呜!呜嗷呜!呜!!!!嗷呜!呜嗷呜嗷呜呜!!!呜嗷!!呜嗷呜呜呜!呜呜嗷!嗷嗷呜嗷!!呜!!嗷!嗷 !嗷
    DecodedData: hello world
    

实例方法调用

使用 System.Buffers.BaseAnyEncoding.CreateEncoder 创建编码器,然后调用其实例方法,对于多次调用会有稍好的性能

  • 使用任意大小字符集(*)作为编码基础

    var charset = "咕噜".AsSpan();
    var source = Encoding.UTF8.GetBytes("hello world");
    
    var encoder = System.Buffers.BaseAnyEncoding.CreateEncoder(charset);
    
    using var encodedData = encoder.Encode(source);
    Console.WriteLine("EncodedData: " + encodedData.Span.ToString());
    
    using var decodedData = encoder.Decode(encodedData.Span);
    Console.WriteLine("DecodedData: " + Encoding.UTF8.GetString(decodedData.Span));
    

    输出为:

    EncodedData: 咕噜噜咕噜咕咕咕咕噜噜咕咕噜咕噜咕噜噜咕噜噜咕咕咕噜噜咕噜噜咕咕咕噜噜咕噜噜噜噜咕咕噜咕咕咕咕咕咕噜噜噜咕 噜噜噜咕噜噜咕噜噜噜噜咕噜噜噜咕咕噜咕咕噜噜咕噜噜咕咕咕噜噜咕咕噜咕咕
    DecodedData: hello world
    

基于自定义类型进行编码

不限于基于 char 编码,可以基于任意类型(需要实现 IEquatable<T> 接口)进行编码,示例:

  • 基于字符串

    var charset = new[] { "富强", "民主", "文明", "和谐", "自由", "平等", "公正", "法治", "爱国", "敬业", "诚信", "友善" };
    var source = Encoding.UTF8.GetBytes("hello world");
    
    var encoder = System.Buffers.BaseAnyEncoding.CreateEncoder(charset);
    
    using var encodedData = encoder.Encode(source);
    Console.WriteLine("EncodedData: " + string.Join(", ", encodedData.Span.ToArray()));
    
    using var decodedData = encoder.Decode(encodedData.Span);
    Console.WriteLine("DecodedData: " + Encoding.UTF8.GetString(decodedData.Span));
    

    输出为:

    EncodedData: 民主, 法治, 富强, 法治, 和谐, 友善, 公正, 友善, 和谐, 敬业, 平等, 公正, 爱国, 友善, 公正, 法治, 法治, 富强, 爱国, 友善, 和谐, 法治, 诚信, 友善, 富强
    DecodedData: hello world
    
  • 基于自定义类型

    record struct TestValue(int Id, string Value)
    {
        public override string ToString() => Id.ToString();
    }
    
    var charset = new TestValue[] { new(0, "今"), new(11, "天"), new(111, "星"), new(1111, "期"), new(11111, "几"), new(111111, "?") };
    var source = Encoding.UTF8.GetBytes("hello world");
    
    var encoder = System.Buffers.BaseAnyEncoding.CreateEncoder(charset);
    
    using var encodedData = encoder.Encode(source);
    Console.WriteLine("EncodedData: " + string.Join(", ", encodedData.Span.ToArray()));
    
    using var decodedData = encoder.Decode(encodedData.Span);
    Console.WriteLine("DecodedData: " + Encoding.UTF8.GetString(decodedData.Span));
    

    输出为:

    EncodedData: 111, 1111, 111111, 0, 111111, 11, 11111, 1111, 11111, 11111, 1111, 0, 111111, 11, 111111, 11, 1111, 111111, 111, 1111, 11111, 1111, 11, 111111, 1111, 11, 111111, 111, 11, 11111, 1111, 11, 11111, 0
    DecodedData: hello world
    
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on BaseAnyEncoding:

Repository Stars
stratosblue/IntelliSenseLocalizer
a tool for generate .net Localized IntelliSense files. 用于生成本地化 .net IntelliSense文件的工具。
Version Downloads Last updated
1.0.1 183 1/15/2024
1.0.0 89 1/15/2024