⭐ Your star is the light at the end of our tunnel. Lead us out of the darkness by starring Byter on GitHub. Star me please, I beg you! 💙 |
powered by ALEC1O
Get basic information about this project called Byter
Overview |
Byter is a C# serialization library for primitive data types, including booleans, numbers, chars, enums, strings, DateTime, BigInteger, bytes, and complex types like classes, structs, arrays, and lists, supporting unlimited complexity and depth. |
---|---|
Website |
Repository: github.com/alec1o/byter |
Contributions |
Thanks a lot <3
|
Official publisher
Nuget | .NET CLI | Netly |
---|---|---|
Install on Nuget |
dotnet add package Byter --version 4.0.0 |
Embedded, Since version 2.x.x |
Notable changes
v1.x.x | v2.x.x | v3.x.x | v4x.x |
---|---|---|---|
Stable | Stable | Stable | Stable |
Main (Reader & Writer) Types:
byte
bool
byte[]
short
ushort
int
uint
long
ulong
float
double
char
string
|
New (Reader & Writer) Types:
Float2 (Vector2)
Float3 (Vector3)
Float4 (Vector4 / Quaternion)
|
Bug Fix. (Reader & Writer)
Support: *Primitive New usage paradigms *Primitive *Primitive Types: bool
byte
char
short
ushort
int
uint
float
long
byte[]
ulong
double
string
*highlights enum*
sbyte*
DateTime*
decimal*
class*
struct*
array*
list*
BigInteger*
|
Bug Fix. (Primitive & Extension)
Support: *Concat Bytes Fix. Compilation warning. |
Used by. Netly v2 | Used by. Netly v3 | Used by. Netly v4 (under dev stage) | Used by. Netly v4 |
Integration and interaction example codes
v1.x.x v2.x.x |
📄 Writer📄 Reader📄 Example
|
---|---|
v3.x.x |
📄 Primitive📄 IPrimitiveAdd📄 IPrimitiveGet📄 Example
|
Byter overhead information
Type | Primitive (overhead + size = total) | Writer/Reader (overhead + size = total) |
---|---|---|
Bool | ✔️ (1 + 1 = 2 bytes) | ✔️ (2 + 1 = 3 bytes) |
Byte | ✔️ (1 + 1 = 2 bytes) | ✔️ (2 + 1 = 3 bytes) |
SByte | ✔️ (1 + 2 = 2 bytes) | 🚫 |
Char | ✔️ (1 + 2 = 3 bytes) | ✔️ (2 + 2 = 4 bytes) |
Short | ✔️ (1 + 2 = 3 bytes) | ✔️ (2 + 2 = 4 bytes) |
UShort | ✔️ (1 + 2 = 3 bytes) | ✔️ (2 + 2 = 4 bytes) |
Int | ✔️ (1 + 4 = 5 bytes) | ✔️ (2 + 4 = 6 bytes) |
UInt | ✔️ (1 + 4 = 5 bytes) | ✔️ (2 + 4 = 6 bytes) |
Float | ✔️ (1 + 4 = 5 bytes) | ✔️ (2 + 4 = 6 bytes) |
Enum | ✔️ (1 + 4 = 5 bytes) | 🚫 |
Long | ✔️ (1 + 8 = 9 bytes) | ✔️ (2 + 8 = 10 bytes) |
ULong | ✔️ (1 + 8 = 9 bytes) | ✔️ (2 + 8 = 10 bytes) |
Double | ✔️ (1 + 8 = 9 bytes) | ✔️ (2 + 8 = 10 bytes) |
DateTime | ✔️ (1 + 8 = 9 bytes) | 🚫 |
Decimal | ✔️ (1 + 16 = 17 bytes) | 🚫 |
String | ✔️ (5 + ? = +5 bytes) *UTF8 | ✔️ (6 + ? = +6 bytes) |
Class | ✔️ (2 + 0 = 2 bytes) | 🚫 |
Struct | ✔️ (2 + 0 = 2 bytes) | 🚫 |
Array | ✔️ (3 + ? = +3 bytes) *Max. 65535 | 🚫 |
List | ✔️ (3 + ? = +3 bytes) *Max. 65535 | 🚫 |
BigInteger | ✔️ (3 + ? = +3 bytes) | 🚫 |
Bytes | ✔️ (5 + ? = +5 bytes) *Max. 4.294.967.295 *(~4billions) | ✔️ (6 + ? = +6 bytes) *Max. 2.147.483.647 *(~2billions) |
using Byter;
-
Convert string to byte[]
// using global encoding (*UTF8) byte[] username = "@alec1o".GetBytes(); // using UNICODE (*UTF16) encoding byte[] message = "Hello 👋 World 🌎".GetBytes(Encoding.Unicode); // using UTF32 encoding string secreatWord = "I'm not human, I'm a concept."; byte[] secreat = secreatWord.GetBytes(Encoding.UTF32);
-
Convert byte[] to string
// using global encoding (*UTF8) string username = new byte[] { ... }.GetString(); // using UNICODE (*UTF16) encoding string message = new byte[] { ... }.GetString(Encoding.Unicode); // using UTF32 encoding byte[] secreat = new byte[] { ... }; string secreatWord = secreat.GetString(Encoding.UTF32);
-
Concat bytes (byte[])
byte[] part1 = [ 1, 1, 1 ]; byte[] part2 = [ 4, 4, 4 ]; /* .. .. .. .. .. .. .. .. .. .. Concat part1 and part2 .. .. .. .. .. .. .. .. .. .. --- `byte[].Concat` Used concat bytes normally. --- `byte[].ConcatInverse` Used concat bytes inversed (first is last and last is first). --- `byte[].Concat(invert: true|false, ....)` Generic way to concat `Inversed` and `Normal` direction. .. .. .. .. .. .. .. .. .. .. */ // Normal >>> byte[] normal = part1.Concat(part2); //... [ 1, 1, 1, 4, 4, 4 ] byte[] normal = part2.Concat(invert: true, part1); //... [ 1, 1, 1, 4, 4, 4 ] byte[] normal = byte[].Concat(invert: true, part2, part1); //... [ 1, 1, 1, 4, 4, 4 ] byte[] normal = byte[].Concat(invert: false, part1, part2); //... [ 1, 1, 1, 4, 4, 4 ] // Inversed <<< byte[] inversed = part1.ConcatInverse(part2); //... [ 4, 4, 4, 1, 1, 1 ] byte[] inversed = part2.Concat(invert: false, part1); //... [ 4, 4, 4, 1, 1, 1 ] byte[] inversed = byte[].Concat(invert: false, part2, part1); //... [ 4, 4, 4, 1, 1, 1 ] byte[] inversed = byte[].Concat(invert: true, part1, part2); //... [ 4, 4, 4, 1, 1, 1 ]
-
Capitalize string
string name = "alECio furanZE".ToCapitalize(); # Alecio Furanze string title = "i'M noT humAn"; title = title.ToCapitalize(); # I'm Not Human
-
UpperCase string
string name = "alECio furanZE".ToUpperCase(); # ALECIO FURANZE string title = "i'M noT humAn"; title = title.ToUpperCase(); # I'M NOT HUMAN
-
LowerCase string
string name = "ALEciO FUraNZE".ToLowerCase(); # alecio furanze string title = "i'M Not huMAN"; title = title.ToLowerCase(); # i'm not human
Legacy Docs (v1.x.x - v2.x.x)
Byter is a bytes serializer. It can serialize and deserialize from primitive type.
-
Nuget SEE HERE
See how add as git submodule? See on bottom of this docsdotnet add package Byter --version 2.0
using Byter
[
`byte`,
`bool`,
`byte[]`,
`short`,
`ushort`,
`int`,
`uint`,
`long`,
`ulong`,
`float`,
`double`,
`char`,
`string`,
`Float2`(Vector2),
`Float3`(Vector3),
`Float4`(Vector4 / Quaternion),
]
Constructor
_ = new Writer(); // Create default instance
_ = new Writer(new Writer()); // Create instance and copy from existing Writer
_ = new Writer(ref new Writer()); // Create instance and copy from existing Writer (using ref)
Proprietary
-
using Byter; var w = new Writer(); // Get lenght of buffer int lenght = w.Length;
Methods
-
using Byter; // Create writer instance; using var w = new Writer(); // Write string w.Write("KEZERO"); // Write char w.Write('K'); // Write Float3 (Vector3) w.Write(new Float3(10F, 10F, 10F)); // Get bytes (buffer) byte[] buffer = w.GetBytes(); // Get byte list (buffer) List<byte> bytes = w.GetList();
-
using Byter; var w = new Writer(); // Return buffer on <Writer> instance byte[] buffer = w.GetBytes();
-
using Byter; var w = new Writer(); // Return buffer on <Writer> instance as byte list List<byte> bytes = w.GetList();
-
using Byter; var w = new Writer(); w.Write((int)1000); w.Write((float)100f); // Clear internal buffer and reset internal index w.Clear();
Constructor
_ = new Reader(new Writer()); // Create instance and copy buffer from existing Writer
_ = new Reader(ref new Writer()); // Create instance and copy buffer from existing Writer (ref Writer)
_ = new Reader(new byte[] { 1, 1, 1, 1 }); // Create instance from buffer (bytes (byte[]))
Proprietary
-
using Byter; byte[] buffer = ...; var r = new Reader(buffer); // Get lenght of buffer int lenght = r.Length;
-
using Byter; byte[] buffer = ...; var r = new Reader(buffer); // return current index of readed buffer int position = r.Position;
-
using Byter; byte[] buffer = ...; var r = new Reader(buffer); string name = r.Read<string>(); int age = r.Read<int>(); // return true if not exist problem on read values. // return false when have any error on read values; bool success = r.Success;
- Internally, before data is written a prefix is added in front of it, so when reading it always compares the prefix of the (data type) you want to read with the strings in the read buffer. if the prefixes do not match then o ( Reader. Success = False), eg. If you write an (int) and try to read a float (Reader.Success = False) because the prefix of an (int) is different from that of a (float), it is recommended to read all the data and at the end check the success, if it is (Reader.Success = False) then one or more data is corrupt. This means that Writer and Reader add dipping to your write and read data.
Methods
-
using Byter; byte[] buffer = ...; // Create reader instance using r = new Reader(buffer); string name = r.Read<string>(); char firstLatter = r.Read<char>(); Float3 position = r.Read<Float3>(); // Check if is success if (r.Success) { Console.WriteLine($"Name: {name}"); Console.WriteLine($"First Latter: {firstLatter}"); Console.WriteLine($"Position: {position}"); } else { Console.WriteLine("Error on get data"); }
-
using Byter; using var w = new Writer(); w.Write("KEZERO"); w.Write((int) 1024); using var r = new Reader(ref w); string name = r.Read<string>(); // name: KEZERO int age = r.Read<int>(); // age: 1024 // Move index (Reader.Position) for target value "MIN VALUE IS 0"; r.Seek(10); // move current index for read for 10 (when start read using .Read<Type> will start read on 10 index from buffer); // Reset internal position r.Seek(0); string name2 = r.Read<string>(); // name: KEZERO (because the start index of this string on buffer is 0) int age2 = r.Read<int>(); age: 1024; // NEED READ LAST INT r.Seek(r.Position - sizeof(int) + sizeof(char) /* int size is 4 + char size is 2. The 2 bytes is overhead of protocol */); int age3 = r.Read<int>(); age: 1024 (because i return 4bytes before old current value)
using Byter;
// writing
Writer writer = new();
writer.Write(1000); // index
writer.Write("{JSON}"); // content
writer.Write(new byte[]{ 1, 1, 1, 1 }); // image
// geting buffer
byte[] buffer = writer.GetBytes();
writer.Dispose(); // Destroy Writer
// reading
Reader reader = new(buffer);
int index = reader.Read<int>();
string json = reader.Read<string>();
byte[] image = reader.Read<byte[]>();
// Check error
if (!reader.Success) // IS FALSE
{
Console.WriteLine("*** ERROR ****");
return;
}
// Check success
Console.WriteLine("*** SUCCESS ****");
// Output
Console.WriteLine($"Index: {index}"); // output: 1000
Console.WriteLine($"JSON : {json }"); // output: JSON
Console.WriteLine($"Image: {image.Length}"); // output: 4
Console.WriteLine($"Status: {reader.Success}"); // output: True
// Making error
float delay = reader.Read<float>();
/*
WARNING:
if you reverse the reading order or try to read more data than added (Reader.Succes = False),
Remembering does not return exception when trying to read data that does not exist it just
returns the default construction, and (Reader.Success) will be assigned (False) */
if (reader.Success) // IS FALSE, THE IS NOT WRITED IN BUFFER
Console.WriteLine($"Delay: {delay}");
else // IS TRUE, THE DELAY NOT EXIST
Console.WriteLine($"Delay not exist");
// Output of status
Console.WriteLine($"Status: {reader.Success}"); // output: False
reader.Dispose(); // Destroy Reader
# Install - recommend a stable branch e.g. "1.x" or use a fork repository, --depth clone last sources
git submodule add --name byter --depth 1 --branch main "https://github.com/alec1o/byter" vendor/byter
# Rebuilding - Download repository and link it in file location, must add this step in dotnet.yaml if using
git submodule update --init
# Update submodule - Update and load new repository updates
git submodule update --remote
# PATH
# |__ vendor
# | |__ byter
# | |__ src
# | |__ Byter.csproj
# |
# |__ app
# | |__ app.csproj
# |
# |__ app.sln
# |__ .git
# |__ .gitignore
# |__ .gitmodules
# .NET link on .sln
cd <PATH>
dotnet sln add vendor/byter/src/Byter.csproj
# .NET link on .csproj
cd app/
dotnet add reference ../vendor/byter/src/Byter.csproj
# Rebuild dependencies to be linked in the project
dotnet restore