Information
exomia/serialization is build for a fast serialization from and to a byte array & more
Example
ByteArray-Utils
using Exomia.Serialization.Utils;
static void Main(string[] args)
{
byte[] buffer = new byte[0];
int offset = BitUtil.Write(ref buffer, 0, true); //offset: 1
offset += BitUtil.Write(ref buffer, offset, int.MaxValue); //offset: 5
offset += BitUtil.Write(ref buffer, offset, int.MaxValue + 99999L); //offset: 13
offset += BitUtil.Write(ref buffer, offset, 34.5f); //offset: 17
offset += BitUtil.Write(ref buffer, offset, "ex"); //offset: 23
Console.WriteLine(string.Join("-", buffer) + " -> " + buffer.Length);
//1-255-255-255-127-158-134-1-128-0-0-0-0-34-0-0-0-4-0-101-0-120-0 -> 23
Console.WriteLine(BitUtil.ReadBoolean(ref buffer, 0)); //True
Console.WriteLine(BitUtil.ReadInt32(ref buffer, 1)); //2147483647
Console.WriteLine(BitUtil.ReadInt64(ref buffer, 5)); //2147583646
Console.WriteLine(BitUtil.ReadSingle(ref buffer, 13)); //34.5
Console.WriteLine(BitUtil.ReadString(ref buffer, 17)); //ex
byte[] buffer2 = new byte[24];
BitUtil.WriteUnsafe(ref buffer2, 0, true);
BitUtil.WriteUnsafe(ref buffer2, 1, int.MaxValue);
BitUtil.WriteUnsafe(ref buffer2, 5, int.MaxValue + 99999L);
BitUtil.WriteUnsafe(ref buffer2, 13, 34.5f);
BitUtil.WriteUnsafe(ref buffer, 17, "ex");
Console.WriteLine(string.Join("-", buffer2) + " -> " + buffer2.Length);
//1-255-255-255-127-158-134-1-128-0-0-0-0-34-0-0-0-4-0-101-0-120-0-0 -> 24
unsafe
{
fixed (byte* ptr = buffer2) //you can use buffer instead of buffer2 here aswell
{
BitUtil.WriteUnsafe(ptr, 0, true);
BitUtil.WriteUnsafe(ptr, 1, int.MaxValue);
BitUtil.WriteUnsafe(ptr, 5, int.MaxValue + 99999L);
BitUtil.WriteUnsafe(ptr, 13, 34.5f);
BitUtil.WriteUnsafe(ptr, 17, "ex");
Console.WriteLine(string.Join("-", buffer2) + " -> " + buffer2.Length);
//1-255-255-255-127-158-134-1-128-0-0-0-0-34-0-0-0-4-0-101-0-120-0-0 -> 24
}
}
unsafe
{
fixed (byte* ptr = buffer2) //you can use buffer instead of buffer2 here aswell
{
Console.WriteLine(BitUtil.ReadBoolean(ptr, 0)); //True
Console.WriteLine(BitUtil.ReadInt32(ptr, 1)); //2147483647
Console.WriteLine(BitUtil.ReadInt64(ptr, 5)); //2147583646
Console.WriteLine(BitUtil.ReadSingle(ptr, 13)); //34.5
Console.WriteLine(BitUtil.ReadString(ptr, 17)); //ex
}
}
}
- several more overloads for Write & WriteUnsafe and the associated read method
- read and write operations also available for arrays
Hashing
using Exomia.Serialization.Hash;
static void Main(string[] args)
{
EHA eha = new EHA(45878);
byte[] byteArray = eha.ToBytes("this is a string");
Console.WriteLine(string.Join("-", byteArray) + " -> " + byteArray.Length);
//233-96-179-194-19-155-53-25-105-91-2-200-213-123-28-143-51-95-208-209-4-106-0-88 -> 24
string hexString = eha.ToString("this is a string");
Console.WriteLine(hexString + " -> " + hexString.Length);
//C2B360E919359B13C8025B698F1C7BD5D1D05F3358006A04 -> 48
}
Installing
[Package Manager]
PM> Install-Package Exomia.Serialization
Changelog
License
MIT License Copyright (c) 2019 exomia
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.