Viyi.Bytes 1.0.0

dotnet add package Viyi.Bytes --version 1.0.0
                    
NuGet\Install-Package Viyi.Bytes -Version 1.0.0
                    
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="Viyi.Bytes" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Viyi.Bytes" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Viyi.Bytes" />
                    
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 Viyi.Bytes --version 1.0.0
                    
#r "nuget: Viyi.Bytes, 1.0.0"
                    
#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.
#:package Viyi.Bytes@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Viyi.Bytes&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Viyi.Bytes&version=1.0.0
                    
Install as a Cake Tool

Viyi.Bytes

Viyi.Bytes is coming soon.

1. 关于 (About)

Viyi.Bytes 是从 Viyi.Util 中分离出来的,专注于处理二进制数据 (bytes) 和其他数据类型(除字符串)之间的转换。

Viyi.Bytes 使用木兰宽松许可证第 2 版 (Mulan Permissive Software License, Version 2)

原 Viyi.Util 涉及的范围非常多,过于分散,而且其中部分工具已经有更好的替代品。所以没有对 Viyi.Util 直接进行升级,而是将其拆分:

  • Viyi.Util V2 完全重构的工具集,对常用数据结构和集合等进行扩展。
  • Viyi.Strings 用于处理字符串 (Viyi.Strings is a string toolkit.)
  • Viyi.Bytes 用于处理二进制数据,计划中 (Viyi.Bytes is a binary data toolkit, in planning.)

API 参考 (API Reference)

本节对库中主要 API 进行简明说明与使用示例,包含字节数组与基础类型相互转换的助手方法(大端/小端)。

BinaryPrimitives 兼容方法

  • ToInt32Le(byte[] source, int start) / ToInt32Be(byte[] source, int start)
  • WriteInt32LittleEndian(byte[] destination, int start, int value) / WriteInt32BigEndian(byte[] destination, int start, int value)

使用示例(大端):

var buf = new byte[4]; BinaryPrimitives.WriteInt32BigEndian(buf, 0, 0x01020304); int v = BinaryPrimitives.ToInt32Be(buf, 0); // v == 0x01020304

使用示例(小端):

var buf = new byte[4]; BinaryPrimitives.WriteInt32LittleEndian(buf, 0, 0x01020304); int v = BinaryPrimitives.ToInt32Le(buf, 0); // v == 0x01020304

库为常用基础类型(例如 Int16/UInt16/Int32/UInt32/Int64/UInt64/Single/Double)生成等价的读写方法,方法命名规则为 To{Type}Be/To{Type}LeWrite{Type}BigEndian/Write{Type}LittleEndian

ByteArrayExtensions 扩展方法

  • byte[]? GetBytes(this IEnumerable<T> values) — 将序列按大端编码为字节数组(长度 sizeof(T)*n)。
  • byte[]? GetBytesLittleEndian(this IEnumerable<T> values) — 小端编码版本。
  • T[]? ToInt32Array(this byte[]? bytes, int start = 0) / ToInt32ArrayLittleEndian(...) — 将字节数组按单元大小转换为类型数组,支持起始位置与自动处理尾部不足字节(会被忽略)。

使用示例:

var arr = new int[] { 1, 2, 3 }; var bytes = arr.GetBytes(); // 大端 var back = bytes.ToInt32Array();

单元测试 (Tests)

当前测试项目使用 MSTest(项目 Viyi.Bytes.Test 已存在并多目标编译)。建议测试要点:

  • 覆盖大端与小端读写的互逆性(写入再读取应还原原值)。
  • 边界条件:起始偏移、长度不足、空数组/空集合。
  • 多目标构建(net8.0net481 / netstandard2.0)应通过测试。

运行测试:

  • Visual Studio: 打开 Test Explorer 并执行 Run All
  • CLI: 使用 dotnet test(会自动为解决方案中的测试项目运行所有目标框架)。

若要添加新的测试规范或样式规则,请将其记录到 CONTRIBUTING.md 中。

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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.  net9.0 was computed.  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.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 112 3/14/2026