Skip to content

Instantly share code, notes, and snippets.

@eriktodx
Created July 25, 2017 16:04
Show Gist options
  • Save eriktodx/0650f7ed66ff8ecc324f48a1fb311273 to your computer and use it in GitHub Desktop.
Save eriktodx/0650f7ed66ff8ecc324f48a1fb311273 to your computer and use it in GitHub Desktop.
Extensions for byte and byte[]
using System;
namespace ErikKralj
{
public static class ByteExtensions
{
public static string ToHex(this byte value)
{
return string.Format("{0:X2}", value);
}
public static string ToHex(this byte[] array1)
{
return ToHex(array1, ' ');
}
public static string ToHex(this byte[] value, char separator)
{
string s = string.Empty;
foreach (byte b in value)
s += string.Format("{0:X2}{1}", b, separator);
return s.Substring(0, s.Length - 1);
}
public static byte[] RotateLeft(this byte[] value)
{
byte[] newValue = new byte[value.Length];
Array.Copy(value, 1, newValue, 0, value.Length - 1);
newValue[newValue.Length - 1] = value[0];
return newValue;
}
public static byte[] RotateRight(this byte[] value)
{
byte[] newValue = new byte[value.Length];
Array.Copy(value, 0, newValue, 1, value.Length - 1);
newValue[0] = value[value.Length - 1];
return newValue;
}
public static bool IsEqual(this byte[] value, byte[] value2)
{
if (value.Length != value2.Length)
return false;
for (int i = 0; i < value.Length; i++)
if (value[i] != value2[i])
return false;
return true;
}
public static bool IsRangeEqual(this byte[] source, int sourceIndex, byte[] destination, int destinationIndex, int count)
{
for (int i = 0; i < count; i++)
if (source[i + sourceIndex] != destination[i + destinationIndex])
return false;
return true;
}
public static byte[] Prepend(this byte[] array1, byte b1)
{
return Prepend(array1, new byte[] { b1 }, 0, 1);
}
public static byte[] Prepend(this byte[] array1, byte[] array2)
{
return Prepend(array1, array2, 0, array2.Length);
}
public static byte[] Prepend(this byte[] array1, byte[] array2, int startIndex)
{
return Prepend(array1, array2, startIndex, array2.Length - startIndex);
}
public static byte[] Prepend(this byte[] array1, byte[] array2, int startIndex, int length)
{
byte[] array3 = new byte[array1.Length + length];
Array.Copy(array2, startIndex, array3, 0, length);
Array.Copy(array1, 0, array3, length, array1.Length);
return array3;
}
public static byte[] Append(this byte[] array1, byte b1)
{
return Append(array1, new byte[] { b1 }, 0, 1);
}
public static byte[] Append(this byte[] array1, byte[] array2)
{
return Append(array1, array2, 0, array2.Length);
}
public static byte[] Append(this byte[] array1, byte[] array2, int startIndex)
{
return Append(array1, array2, startIndex, array2.Length - startIndex);
}
public static byte[] Append(this byte[] array1, byte[] array2, int startIndex, int length)
{
byte[] array3 = new byte[array1.Length + length];
Array.Copy(array1, array3, array1.Length);
Array.Copy(array2, startIndex, array3, array1.Length, length);
return array3;
}
public static byte[] Subbytes(this byte[] source, int startIndex)
{
return Subbytes(source, startIndex, source.Length - startIndex);
}
public static byte[] Subbytes(this byte[] source, int startIndex, int length)
{
byte[] subbytes = new byte[length];
Array.Copy(source, startIndex, subbytes, 0, length);
return subbytes;
}
public static byte[] ToArray(this byte b1)
{
return new byte[] { b1 };
}
public static byte[] PadLeft(this byte[] array1, int totalLength)
{
return PadLeft(array1, totalLength, 0x00);
}
public static byte[] PadLeft(this byte[] array1, int totalLength, byte paddingByte)
{
byte[] array2 = new byte[Math.Max(array1.Length, totalLength)];
Array.Copy(array1, 0, array2, array2.Length - array1.Length, array1.Length);
for (int i = 0; i < array2.Length - array1.Length; i++)
array2[i] = paddingByte;
return array2;
}
public static byte[] PadRight(this byte[] array1, int totalLength)
{
return PadRight(array1, totalLength, 0x00);
}
public static byte[] PadRight(this byte[] array1, int totalLength, byte paddingByte)
{
byte[] array2 = new byte[Math.Max(array1.Length, totalLength)];
Array.Copy(array1, 0, array2, 0, array1.Length);
for (int i = array1.Length; i < array2.Length; i++)
array2[i] = paddingByte;
return array2;
}
public static byte[] Reverse(this byte[] array1)
{
byte[] array2 = new byte[array1.Length];
Array.Copy(array1, array2, array2.Length);
Array.Reverse(array2);
return array2;
}
public static byte[] Resize(this byte[] array1, int newSize)
{
byte[] array2 = new byte[newSize];
Array.Copy(array1, array2, Math.Min(newSize, array1.Length));
return array2;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment