laatzen/Common/Utils/Common.Utils.Extensions/BytesExtensions.cs
2024-10-17 10:06:36 +02:00

327 lines
8.7 KiB
C#

namespace Common.Utils.Extensions
{
using System;
using System.Collections.Generic;
public static class BytesExtensions
{
public static Int16 ToInt16BE(this Byte[] bytes, Int32 start)
{
var result = default(Int16);
result ^= (Int16)(bytes[start++] << 8);
result ^= bytes[start++];
return result;
}
public static Int32 ToInt32BE(this Byte[] bytes, Int32 start)
{
var result = default(Int32);
result ^= bytes[start++] << 24;
result ^= bytes[start++] << 16;
result ^= bytes[start++] << 8;
result ^= bytes[start++];
return result;
}
public static Int64 ToInt64BE(this Byte[] bytes, Int32 start)
{
var result = default(Int64);
result ^= bytes[start++] << 56;
result ^= bytes[start++] << 48;
result ^= bytes[start++] << 40;
result ^= bytes[start++] << 32;
result ^= bytes[start++] << 24;
result ^= bytes[start++] << 16;
result ^= bytes[start++] << 8;
result ^= bytes[start++];
return result;
}
public static UInt16 ToUInt16BE(this Byte[] bytes, Int32 start)
{
var result = default(UInt16);
result ^= (UInt16)(bytes[start++] << 8);
result ^= bytes[start++];
return result;
}
public static UInt32 ToUInt32BE(this Byte[] bytes, Int32 start)
{
var result = default(UInt32);
result ^= (UInt32)(bytes[start++] << 24);
result ^= (UInt32)(bytes[start++] << 16);
result ^= (UInt32)(bytes[start++] << 8);
result ^= bytes[start++];
return result;
}
public static UInt64 ToUInt64BE(this Byte[] bytes, Int32 start)
{
var result = default(UInt64);
result ^= (UInt64)(bytes[start++] << 56);
result ^= (UInt64)(bytes[start++] << 48);
result ^= (UInt64)(bytes[start++] << 40);
result ^= (UInt64)(bytes[start++] << 32);
result ^= (UInt64)(bytes[start++] << 24);
result ^= (UInt64)(bytes[start++] << 16);
result ^= (UInt64)(bytes[start++] << 8);
result ^= bytes[start++];
return result;
}
public static Int16 ToInt16LE(this Byte[] bytes, Int32 start)
{
var result = default(Int16);
start += 2;
result ^= (Int16)(bytes[start--] << 8);
result ^= bytes[start--];
return result;
}
public static Int32 ToInt32LE(this Byte[] bytes, Int32 start)
{
var result = default(Int32);
start += 4;
result ^= bytes[start--] << 24;
result ^= bytes[start--] << 16;
result ^= bytes[start--] << 8;
result ^= bytes[start--];
return result;
}
public static Int64 ToInt64LE(this Byte[] bytes, Int32 start)
{
var result = default(Int64);
start += 8;
result ^= bytes[start--] << 56;
result ^= bytes[start--] << 48;
result ^= bytes[start--] << 40;
result ^= bytes[start--] << 32;
result ^= bytes[start--] << 24;
result ^= bytes[start--] << 16;
result ^= bytes[start--] << 8;
result ^= bytes[start--];
return result;
}
public static UInt16 ToUInt16LE(this Byte[] bytes, Int32 start)
{
var result = default(UInt16);
start += 2;
result ^= (UInt16)(bytes[start--] << 8);
result ^= bytes[start--];
return result;
}
public static UInt32 ToUInt32LE(this Byte[] bytes, Int32 start)
{
var result = default(UInt32);
start += 4;
result ^= (UInt32)(bytes[start--] << 24);
result ^= (UInt32)(bytes[start--] << 16);
result ^= (UInt32)(bytes[start--] << 8);
result ^= bytes[start--];
return result;
}
public static UInt64 ToUInt64LE(this Byte[] bytes, Int32 start)
{
var result = default(UInt64);
start += 8;
result ^= (UInt64)(bytes[start--] << 56);
result ^= (UInt64)(bytes[start--] << 48);
result ^= (UInt64)(bytes[start--] << 40);
result ^= (UInt64)(bytes[start--] << 32);
result ^= (UInt64)(bytes[start--] << 24);
result ^= (UInt64)(bytes[start--] << 16);
result ^= (UInt64)(bytes[start--] << 8);
result ^= bytes[start--];
return result;
}
public static Byte[] GetBytesBE(this UInt16 value)
{
var bytes = new Byte[2];
bytes[0] = (Byte)(value >> 8);
bytes[1] = (Byte)value;
return bytes;
}
public static Byte[] GetBytesBE(this UInt32 value)
{
var bytes = new Byte[2];
bytes[0] = (Byte)(value >> 24);
bytes[1] = (Byte)(value >> 16);
bytes[2] = (Byte)(value >> 8);
bytes[3] = (Byte)value;
return bytes;
}
public static Byte[] GetBits(this Byte @byte)
{
var bits = new Byte[8];
for (var i = 0; i < 8; i++)
{
bits[i] = (Byte)((@byte >> i) & 1);
}
return bits;
}
public static void SetBits(this Byte value, Byte[] bits, Int32 start, Int32 count)
{
for (var i = 0; i < count; i++)
{
bits[start + i] = (Byte)((value >> i) & 1);
}
}
public static Byte ToByte(this Byte[] bits)
{
var _byte = default(Int32);
for (var i = 0; i < 8; i++)
{
_byte |= bits[i] << i;
}
return (Byte)_byte;
}
public static Byte ToByte(this Byte[] bits, Int32 start, Int32 count)
{
var sum = 0;
for (var i = 0; i < count; i++)
{
sum |= bits[start + i] << i;
}
return (Byte)sum;
}
public static Byte[] ToByteArray(this String hexString)
{
var bytes = new List<Byte>();
if (!String.IsNullOrWhiteSpace(hexString))
{
var first = true;
var x = 0;
foreach (var hex in hexString)
{
if ('0' <= hex && hex <= '9')
{
x += hex - '0';
}
else if ('A' <= hex && hex <= 'F')
{
x += hex - 'A' + 10;
}
else if ('a' <= hex && hex <= 'f')
{
x = hex - 'a' + 10;
}
else
{
continue;
}
if (first)
{
x *= 16;
}
else
{
bytes.Add((Byte)x);
x = 0;
}
first = !first;
}
}
return bytes.ToArray();
}
public static Byte[] GetReversed(this Byte[] bytes, Int32 start, Int32 size)
{
var _bytes = new Byte[size];
Array.Copy(bytes, start, _bytes, 0, size);
Array.Reverse(_bytes);
return _bytes;
}
public static Int32 ToInt32BigEndian(this Byte[] bytes, Int32 start)
{
var _bytes = bytes.GetReversed(start, sizeof(Int32));
return BitConverter.ToInt32(_bytes, 0);
}
public static UInt16 ToUInt16BigEndian(this Byte[] bytes, Int32 start)
{
var _bytes = bytes.GetReversed(start, sizeof(UInt16));
return BitConverter.ToUInt16(_bytes, 0);
}
public static UInt32 ToUInt32BigEndian(this Byte[] bytes, Int32 start)
{
var _bytes = bytes.GetReversed(start, sizeof(UInt32));
return BitConverter.ToUInt32(_bytes, 0);
}
public static Byte[] Reverse(this Byte[] bytes)
{
Array.Reverse(bytes);
return bytes;
}
public static Byte[] ToBigEndianBytes(this Int32 value)
=> BitConverter.GetBytes(value).Reverse();
public static Byte[] ToBigEndianBytes(this UInt32 value)
=> BitConverter.GetBytes(value).Reverse();
}
}