From ad9157a0ca1b647c8b7707001a9279231aafb2d5 Mon Sep 17 00:00:00 2001 From: Harald Wolff Date: Mon, 4 Nov 2019 10:01:59 +0100 Subject: [PATCH] WIP --- ByteArrayExtensions.cs | 75 ++++++++++++++++++++++++++++++++++++++++++ Extensions.cs | 7 ++-- ln.types.csproj | 1 + 3 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 ByteArrayExtensions.cs diff --git a/ByteArrayExtensions.cs b/ByteArrayExtensions.cs new file mode 100644 index 0000000..bfce07b --- /dev/null +++ b/ByteArrayExtensions.cs @@ -0,0 +1,75 @@ +// /** +// * File: ByteArrayExtensions.cs +// * Author: haraldwolff +// * +// * This file and it's content is copyrighted by the Author and / or copyright holder. +// * Any use wihtout proper permission is illegal and may lead to legal actions. +// * +// * +// **/ +using System; +namespace ln.types +{ + public enum Endianess { BIG, LITTLE } + + public static class ByteArrayExtensions + { + + public static byte[] To(this byte[] bytes,Endianess endianess) + { + if (BitConverter.IsLittleEndian == (endianess==Endianess.BIG)) + Array.Reverse(bytes); + return bytes; + } + + public static byte GetByte(this byte[] bytes, ref int offset) => bytes[offset++]; + public static byte[] GetByte(this byte[] bytes,ref int offset,out byte value) + { + value = GetByte(bytes, ref offset); + return bytes; + } + + + public static byte[] GetShort(this byte[] bytes, ref int offset, out short value) => GetShort(bytes, ref offset, out value, Endianess.LITTLE); + public static byte[] GetShort(this byte[] bytes, ref int offset, out short value, Endianess endianess) + { + value = GetShort(bytes, ref offset, endianess); + return bytes; + } + public static short GetShort(this byte[] bytes, ref int offset, Endianess endianess) => BitConverter.ToInt16(bytes.Slice(ref offset, 2).To(endianess), 0); + + public static byte[] GetUShort(this byte[] bytes, ref int offset, out ushort value) => GetUShort(bytes, ref offset, out value, Endianess.LITTLE); + public static byte[] GetUShort(this byte[] bytes, ref int offset, out ushort value, Endianess endianess) + { + value = GetUShort(bytes, ref offset, endianess); + return bytes; + } + public static ushort GetUShort(this byte[] bytes, ref int offset, Endianess endianess) => BitConverter.ToUInt16(bytes.Slice(ref offset, 2).To(endianess), 0); + + public static byte[] GetInt(this byte[] bytes, ref int offset, out int value) => GetInt(bytes, ref offset, out value, Endianess.LITTLE); + public static byte[] GetInt(this byte[] bytes, ref int offset, out int value, Endianess endianess) + { + value = GetInt(bytes, ref offset, endianess); + return bytes; + } + public static int GetInt(this byte[] bytes, ref int offset, Endianess endianess) => BitConverter.ToInt32(bytes.Slice(ref offset, 4).To(endianess), 0); + + public static byte[] GetUInt(this byte[] bytes, ref int offset, out uint value) => GetUInt(bytes, ref offset, out value, Endianess.LITTLE); + public static byte[] GetUInt(this byte[] bytes, ref int offset, out uint value, Endianess endianess) + { + value = GetUInt(bytes, ref offset, endianess); + return bytes; + } + public static uint GetUInt(this byte[] bytes, ref int offset, Endianess endianess) => BitConverter.ToUInt32(bytes.Slice(ref offset, 4).To(endianess), 0); + + + public static byte[] GetBytes(this byte[] bytes,ref int offset,int length,out byte[] value) + { + value = bytes.Slice(ref offset, length); + return bytes; + } + + + + } +} diff --git a/Extensions.cs b/Extensions.cs index 4a60411..12c3a3b 100644 --- a/Extensions.cs +++ b/Extensions.cs @@ -256,14 +256,17 @@ namespace ln.types public static T[] Slice(this T[] me, int offset) => Slice(me, offset, me.Length - offset); - public static T[] Slice(this T[] me, int offset, int length) + public static T[] Slice(this T[] me, ref int offset) => Slice(me, ref offset, me.Length - offset); + public static T[] Slice(this T[] me, int offset, int length) => Slice(me, ref offset, length); + public static T[] Slice(this T[] me, ref int offset, int length) { T[] slice = new T[length]; Array.Copy(me, offset, slice, 0, length); + offset += length; return slice; } - public static byte[] BytesFromHexString(string hexString) + public static byte[] BytesFromHexString(string hexString) { int NumberChars = hexString.Length; byte[] bytes = new byte[NumberChars / 2]; diff --git a/ln.types.csproj b/ln.types.csproj index 30422cb..4692b1e 100644 --- a/ln.types.csproj +++ b/ln.types.csproj @@ -128,6 +128,7 @@ +