You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A C# library that can read and write primitives, enums, arrays, and strings to streams using specified endianness, string encoding, and boolean sizes.
7
-
Objects can also be read from/written to streams via reflection and attributes.
6
+
This .NET library provides a simple API to read/write bytes from/to streams and spans using user-specified endianness.
7
+
By default, supported types include primitives, enums, arrays, strings, and some common .NET struct types.
8
+
Objects can also be read/written from/to streams via reflection and attributes.
9
+
The developer can use the API even if their target behavior or data is not directly supported by using the `IBinarySerializable` interface, inheritting from the reader/writer, or using the manual `Span<T>`/`ReadOnlySpan<T>` methods without streams.
10
+
Performance is the focus when not using reflection; no allocations unless absolutely necessary!
8
11
9
-
The `IBinarySerializable` interface allows an object to be read and written in a customizable fashion.
12
+
The `IBinarySerializable` interface allows an object to be read and written in a customizable fashion during reflection.
10
13
Also included are attributes that can make reading and writing objects less of a headache.
11
14
For example, classes and structs in C# cannot have ignored members when marshalling, but **EndianBinaryIO** has a `BinaryIgnoreAttribute` that will ignore properties when reading and writing.
12
15
13
-
There is also an `EndianBitConverter` static class which resembles `System.BitConverter`. With it you can convert to/from data types using arrays rather than streams, all with specific endianness.
16
+
The `EndianBinaryPrimitives` static class which resembles `System.Buffers.Binary.BinaryPrimitives` is an API that converts to/from data types using `Span<T>`/`ReadOnlySpan<T>` with specific endianness, rather than streams.
17
+
18
+
----
19
+
## Changelog For v2.0.0.0
20
+
Be sure to check the comment at https://github.com/Kermalis/EndianBinaryIO/pull/28!
14
21
15
22
----
16
23
## 🚀 Usage:
@@ -23,47 +30,46 @@ Assume we have the following definitions:
23
30
```cs
24
31
enumByteSizedEnum : byte
25
32
{
26
-
Val1=0x20,
27
-
Val2=0x80
33
+
Val1=0x20,
34
+
Val2=0x80,
28
35
}
29
36
enumShortSizedEnum : short
30
37
{
31
-
Val1=0x40,
32
-
Val2=0x800
38
+
Val1=0x40,
39
+
Val2=0x800,
33
40
}
34
41
35
42
classMyBasicObj
36
43
{
37
-
// Properties
38
-
publicShortSizedEnumType { get; set; }
39
-
publicshortVersion { get; set; }
40
-
publicDateTimeDate { get; set; }
41
-
42
-
// Property that is ignored when reading and writing
43
-
[BinaryIgnore(true)]
44
-
publicByteSizedEnumDoNotReadOrWrite { get; set; }
45
-
46
-
// Arrays work as well
47
-
[BinaryArrayFixedLength(16)]
48
-
publicuint[] ArrayWith16Elements { get; set; }
49
-
50
-
// Boolean that occupies 4 bytes instead of one
51
-
[BinaryBooleanSize(BooleanSize.U32)]
52
-
publicboolBool32 { get; set; }
53
-
54
-
// String encoded in ASCII
55
-
// Reads chars until the stream encounters a '\0'
56
-
// Writing will append a '\0' at the end of the string
// String encoded in UTF16-LE that will only read/write 10 chars
62
-
// The BinaryStringTrimNullTerminatorsAttribute will indicate that every char from the first \0 will be removed from the string. This attribute also works with char arrays
63
-
[BinaryEncoding("UTF-16")]
64
-
[BinaryStringFixedLength(10)]
65
-
[BinaryStringTrimNullTerminators(true)]
66
-
publicstringUTF16String { get; set; }
44
+
// Properties
45
+
publicShortSizedEnumType { get; set; }
46
+
publicshortVersion { get; set; }
47
+
publicDateTimeDate { get; set; }
48
+
49
+
// Property that is ignored when reading and writing
50
+
[BinaryIgnore]
51
+
publicByteSizedEnumDoNotReadOrWrite { get; set; }
52
+
53
+
// Arrays work as well
54
+
[BinaryArrayFixedLength(16)]
55
+
publicuint[] ArrayWith16Elements { get; set; }
56
+
57
+
// Boolean that occupies 4 bytes instead of one
58
+
[BinaryBooleanSize(BooleanSize.U32)]
59
+
publicboolBool32 { get; set; }
60
+
61
+
// String encoded in ASCII
62
+
// Reads chars until the stream encounters a '\0'
63
+
// Writing will append a '\0' at the end of the string
// String encoded in UTF16-LE that will only read/write 10 chars
69
+
// The BinaryStringTrimNullTerminatorsAttribute will indicate that every char from the first \0 will be removed from the string. This attribute also works with char arrays
70
+
[BinaryStringFixedLength(10)]
71
+
[BinaryStringTrimNullTerminators]
72
+
publicstringUTF16String { get; set; }
67
73
}
68
74
```
69
75
And assume these are our input bytes (in little endian):
@@ -107,89 +113,93 @@ obj.Type = reader.ReadEnum<ShortSizedEnum>(); // Reads the enum type based on th
107
113
obj.Version=reader.ReadInt16(); // Reads a 'short' (2 bytes)
108
114
obj.Date=reader.ReadDateTime(); // Reads a 'DateTime' (8 bytes)
obj.Bool32=reader.ReadBoolean(); // Reads a 'bool' (4 bytes in this case, since the reader was initiated with a default of BooleanSize.U32, but there is an overload to pass in one)
119
+
obj.Bool32=reader.ReadBoolean(); // Reads a 'bool' (4 bytes in this case, since the reader's current bool state is BooleanSize.U32)
113
120
114
-
obj.NullTerminatedASCIIString=reader.ReadStringNullTerminated(Encoding.ASCII); // Reads ASCII chars until a '\0' is read, then returns a 'string'
115
-
obj.UTF16String=reader.ReadString(10, true, Encoding.Unicode); // Reads 10 UTF16-LE chars as a 'string' with the '\0's removed
121
+
reader.ASCII=true; // Set the reader's ASCII state to true
122
+
obj.NullTerminatedASCIIString=reader.ReadString_NullTerminated(); // Reads ASCII chars until a '\0' is read, then returns a 'string'
123
+
124
+
reader.ASCII=false; // Set the reader's ASCII state to false (UTF16-LE)
125
+
obj.UTF16String=reader.ReadString_Count_TrimNullTerminators(10); // Reads 10 UTF16-LE chars as a 'string' with the '\0's removed
writer.Write(obj.Bool32); // Writes a 'bool' (4 bytes in this case, since the reader was initiated with a default of BooleanSize.U32, but there is an overload to pass in one)
151
-
writer.Write(obj.NullTerminatedASCIIString, true, Encoding.ASCII); // Writes the chars in the 'string' as ASCII and appends a '\0' at the end
152
-
writer.Write(obj.UTF16String, 10, Encoding.Unicode); // Writes 10 UTF16-LE chars as a 'string'. If the string has more than 10 chars, it is truncated; if it has less, it is padded with '\0'
156
+
writer.WriteEnum(obj.Type); // Writes the enum type based on the amount of bytes of the enum's underlying type (short/2 in this case)
157
+
writer.WriteInt16(obj.Version); // Writes a 'short' (2 bytes)
158
+
writer.WriteDateTime(obj.Date); // Writes a 'DateTime' (8 bytes)
writer.WriteBoolean(obj.Bool32); // Writes a 'bool' (4 bytes in this case, since the reader's current bool state is BooleanSize.U32)
161
+
162
+
writer.ASCII=true; // Set the reader's ASCII state to true
163
+
writer.WriteChars_NullTerminated(obj.NullTerminatedASCIIString); // Writes the chars in the 'string' as ASCII and appends a '\0' at the end
164
+
165
+
writer.ASCII=false; // Set the reader's ASCII state to false (UTF16-LE)
166
+
writer.WriteChars_Count(obj.UTF16String, 10); // Writes 10 UTF16-LE chars as a 'string'. If the string has more than 10 chars, it is truncated; if it has less, it is padded with '\0'
0 commit comments