BinaryHelper 1.0.0

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

Binary Helper

Класс BinaryConverter из пакета BinaryHelper предназначен для конвертации значений полей/свойств классов в массив байт и обратно из массива байт в значения полей/свойств.

Примеры использования

Предположим, у нас есть класс Test:

class Test
{
[Binary]
public int a;

[Binary]
public byte B { get; set; }

[Binary(Count = 10)]
public string C { get; set; } = "Тест";
}

Кодирование в массив байтов

Test test = new Test() {a = 5, B = 1};
byte[] result = BinaryConverter.ToBytes(test);

Данный пример преобразует значения полей из экземпляра класса test в массив байтов, в результате чего в переменной result будет содержаться массив

[0x05, 0x00, 0x00, 0x00, 0x01, 0xd0, 0xa2, 0xd0, 0xb5, 0xd1, 0x81, 0xd1, 0x82, 0x20, 0x20]

Первые байты 0x05, 0x00, 0x00, 0x00 соответствуют значению a = 5, байт 0x01 соответствует B = 1, байты 0xd0, 0xa2, 0xd0, 0xb5, 0xd1, 0x81, 0xd1, 0x82 соответствуют строке C = "Тест", назначенной в классе по умолчанию. Последние 2 байта 0x20, 0x20 соответствуют двум символам пробела, чтобы длина последовательности составила 10 байт, так как в атрибуте для поля C явно задана длина 10:

[Binary(Count = 10)]
public string C { get; set; } = "Тест";

В случае, если длина массива байтов, полученных из строки, будет превышать 10, лишние байты будут отсечены. Это может привести к тому, что, например, в кодировке UTF8 последний символ при обратном декодировании может оказаться "битым".

Декодирование в класс из массива байтов

Данный пример преобразует массив байт, хранящийся в переменной data, в значения полей и свойств класса Test, создав его экземпляр в переменной test.

byte[] data = new byte[] {0x05, 0x00, 0x00, 0x00, 0x01, 0xd0, 0xa2, 0xd0, 0xb5, 0xd1, 0x81, 0xd1, 0x82, 0x20, 0x20};
Test result = BinaryConverter.FromBytes<Test>(data);

Данный пример преобразует из массива байт в класс Test , в результате чего в переменной result будет содержаться класс Test

result.a == 5
result.B == 1
result.C == "Тест"
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 was computed.  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 was computed.  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.
  • .NETStandard 2.0

    • No dependencies.

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 1,174 5/16/2018