Skip to content

Instantly share code, notes, and snippets.

@mjs3339
Last active April 28, 2018 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjs3339/dfd6286870d8c25403de6e76112738e1 to your computer and use it in GitHub Desktop.
Save mjs3339/dfd6286870d8c25403de6e76112738e1 to your computer and use it in GitHub Desktop.
C# GetBytes from Primitive Data Types and Arrays
public static class GetBytesEx
{
/// <summary>
/// Get Bytes from a single boolean object
/// </summary>
public static byte[] GetBytes(this bool obj)
{
return new[] {obj ? (byte) 1 : (byte) 0};
}
/// <summary>
/// Get Bytes from an array of boolean objects
/// </summary>
public static byte[] GetBytes(this bool[] obj)
{
if (obj == null)
throw new Exception("GetBytes (bool[]) object cannot be null.");
var seed = new byte[0];
return obj.Aggregate(seed, (Current, bl) => Current.Add(bl.GetBytes()));
}
/// <summary>
/// Get Bytes from a single byte object
/// </summary>
public static byte[] GetBytes(this byte obj)
{
return new[] {obj};
}
/// <summary>
/// Get Bytes from a sbyte short object
/// </summary>
[SecuritySafeCritical]
public static unsafe byte[] GetBytes(this sbyte obj)
{
var numArray = new byte[1];
fixed (byte* numPtr = numArray)
{
*(sbyte*) numPtr = obj;
}
return numArray;
}
/// <summary>
/// Get Bytes from an array of sbyte objects
/// </summary>
public static byte[] GetBytes(this sbyte[] obj)
{
if (obj == null)
throw new Exception("GetBytes (sbyte[]) object cannot be null.");
var numArray = new byte[obj.Length];
Buffer.BlockCopy(obj, 0, numArray, 0, numArray.Length);
return numArray;
}
/// <summary>
/// Get Bytes from a single short object
/// </summary>
[SecuritySafeCritical]
public static unsafe byte[] GetBytes(this short obj)
{
var numArray = new byte[2];
fixed (byte* numPtr = numArray)
{
*(short*) numPtr = obj;
}
return numArray;
}
/// <summary>
/// Get Bytes from an array of short objects
/// </summary>
public static byte[] GetBytes(this short[] obj)
{
if (obj == null)
throw new Exception("GetBytes (short[]) object cannot be null.");
var numArray = new byte[obj.Length * 2];
Buffer.BlockCopy(obj, 0, numArray, 0, numArray.Length);
return numArray;
}
/// <summary>
/// Get Bytes from a single unsigned short object
/// </summary>
public static byte[] GetBytes(this ushort obj)
{
return ((short) obj).GetBytes();
}
/// <summary>
/// Get Bytes from an array of unsigned short objects
/// </summary>
public static byte[] GetBytes(this ushort[] obj)
{
if (obj == null)
throw new Exception("GetBytes (ushort[]) object cannot be null.");
var numArray = new byte[obj.Length * 2];
Buffer.BlockCopy(obj, 0, numArray, 0, numArray.Length);
return numArray;
}
/// <summary>
/// Get Bytes from a single character object
/// </summary>
public static byte[] GetBytes(this char obj)
{
return ((short) obj).GetBytes();
}
/// <summary>
/// Get Bytes from an array of character objects
/// </summary>
public static byte[] GetBytes(this char[] obj)
{
if (obj == null)
throw new Exception("GetBytes (char[]) object cannot be null.");
var numArray = new byte[obj.Length * 2];
Buffer.BlockCopy(obj, 0, numArray, 0, numArray.Length);
return numArray;
}
/// <summary>
/// Get Bytes from a single integer object
/// </summary>
[SecuritySafeCritical]
public static unsafe byte[] GetBytes(this int obj)
{
var numArray = new byte[4];
fixed (byte* numPtr = numArray)
{
*(int*) numPtr = obj;
}
return numArray;
}
public static byte[] GetBytes(this int obj, int sIndex, int count)
{
if (count > 8)
throw new Exception("Size cannot exceed 4 bytes.");
return obj.GetBytes().SubArray(sIndex, count);
}
/// <summary>
/// Get Bytes from an array of integer objects
/// </summary>
public static byte[] GetBytes(this int[] obj)
{
if (obj == null)
throw new Exception("GetBytes (int[]) object cannot be null.");
var numArray = new byte[obj.Length * 4];
Buffer.BlockCopy(obj, 0, numArray, 0, numArray.Length);
return numArray;
}
/// <summary>
/// Get Bytes from a single unsigned integer object
/// </summary>
public static byte[] GetBytes(this uint obj)
{
return ((int) obj).GetBytes();
}
public static byte[] GetBytes(this uint obj, int sIndex = 0, int count = 4)
{
if (count > 8)
throw new Exception("Size cannot exceed 4 bytes.");
return obj.GetBytes().SubArray(sIndex, count);
}
/// <summary>
/// Get Bytes from an array of unsigned integer objects
/// </summary>
public static byte[] GetBytes(this uint[] obj)
{
if (obj == null)
throw new Exception("GetBytes (uint[]) object cannot be null.");
var numArray = new byte[obj.Length * 4];
Buffer.BlockCopy(obj, 0, numArray, 0, numArray.Length);
return numArray;
}
/// <summary>
/// Get Bytes from a single long object
/// </summary>
public static unsafe byte[] GetBytes(this long obj)
{
var numArray = new byte[8];
fixed (byte* numPtr = numArray)
{
*(long*) numPtr = obj;
}
return numArray;
}
/// <summary>
/// Get Bytes from an array of long objects
/// </summary>
public static byte[] GetBytes(this long[] obj)
{
if (obj == null)
throw new Exception("GetBytes (long[]) object cannot be null.");
var numArray = new byte[obj.Length * 8];
Buffer.BlockCopy(obj, 0, numArray, 0, numArray.Length);
return numArray;
}
public static byte[] GetBytes(this long obj, int sIndex = 0, int count = 8)
{
if (count > 8)
throw new Exception("Size cannot exceed 8 bytes.");
return obj.GetBytes().SubArray(sIndex, count);
}
/// <summary>
/// Get Bytes from a single unsigned long object
/// </summary>
public static byte[] GetBytes(this ulong obj)
{
return ((long) obj).GetBytes();
}
public static byte[] GetBytes(this ulong obj, int sIndex = 0, int count = 8)
{
if (count > 8)
throw new Exception("Size cannot exceed 8 bytes.");
return ((long) obj).GetBytes().SubArray(sIndex, count);
}
/// <summary>
/// Get Bytes from an array of unsigned long objects
/// </summary>
public static byte[] GetBytes(this ulong[] obj)
{
if (obj == null)
throw new Exception("GetBytes (ulong[]) object cannot be null.");
var numArray = new byte[obj.Length * 8];
Buffer.BlockCopy(obj, 0, numArray, 0, numArray.Length);
return numArray;
}
/// <summary>
/// Get Bytes from a single float object
/// </summary>
[SecuritySafeCritical]
public static unsafe byte[] GetBytes(this float obj)
{
return *(int*) &obj.GetBytes();
}
/// <summary>
/// Get Bytes from an array of float objects
/// </summary>
public static byte[] GetBytes(this float[] obj)
{
if (obj == null)
throw new Exception("GetBytes (float[]) object cannot be null.");
var numArray = new byte[obj.Length * 4];
Buffer.BlockCopy(obj, 0, numArray, 0, numArray.Length);
return numArray;
}
/// <summary>
/// Get Bytes from a single double object
/// </summary>
[SecuritySafeCritical]
public static unsafe byte[] GetBytes(this double obj)
{
return *(long*) &obj.GetBytes();
}
/// <summary>
/// Get Bytes from an array of double objects
/// </summary>
public static byte[] GetBytes(this double[] obj)
{
if (obj == null)
throw new Exception("GetBytes (double[]) object cannot be null.");
var numArray = new byte[obj.Length * 8];
Buffer.BlockCopy(obj, 0, numArray, 0, numArray.Length);
return numArray;
}
/// <summary>
/// Get Bytes from a single string object
/// </summary>
public static byte[] GetBytes(this string obj, Encoding enc = null)
{
if (enc == null)
return Encoding.Default.GetBytes(obj);
switch (enc)
{
case ASCIIEncoding AsciiEncoding:
return Encoding.ASCII.GetBytes(obj);
break;
case UnicodeEncoding UnicodeEncoding:
return Encoding.Unicode.GetBytes(obj);
break;
case UTF32Encoding Utf32Encoding:
return Encoding.UTF32.GetBytes(obj);
break;
case UTF7Encoding Utf7Encoding:
return Encoding.UTF7.GetBytes(obj);
break;
case UTF8Encoding Utf8Encoding:
return Encoding.UTF8.GetBytes(obj);
break;
default:
throw new ArgumentOutOfRangeException(nameof(enc));
}
}
/// <summary>
/// Get Bytes from a single string object. Does not use any extended conversion 8 bits not 16 like above
/// </summary>
public static byte[] GetBytesASCII(this string obj)
{
var b = new byte[obj.Length];
var ptr = 0;
foreach (var v in obj)
b[ptr++] = (byte) v;
return b;
}
/// <summary>
/// Get Bytes from a array of string objects. Does not use any extended conversion 8 bits not 16 like below
/// </summary>
public static byte[] GetBytesASCII(this string[] obj)
{
if (obj == null)
throw new Exception("GetBytes (string[]) object cannot be null.");
var numArray = new byte[obj.Where(ss => ss != null).Sum(ss => ss.Length)];
var dstOffset = 0;
foreach (var str in obj)
if (str != null)
{
Buffer.BlockCopy(str.GetBytesASCII(), 0, numArray, dstOffset, str.Length);
dstOffset += str.Length;
}
return numArray;
}
/// <summary>
/// Get Bytes from an array of string objects
/// </summary>
public static byte[] GetBytes(this string[] obj, Encoding enc = null)
{
if (obj == null)
throw new Exception("GetBytes (string[]) object cannot be null.");
var tb = obj[0].GetBytes(enc);
var cs = tb.Length / obj[0].Length;
var numArray = new byte[obj.Where(ss => ss != null).Sum(ss => ss.Length) * cs];
var dstOffset = 0;
foreach (var str in obj)
if (str != null)
{
var buf = str.GetBytes(enc);
Buffer.BlockCopy(buf, 0, numArray, dstOffset, buf.Length);
dstOffset += buf.Length;
}
return numArray;
}
/// <summary>
/// Get Bytes from a single secure string object
/// </summary>
public static byte[] GetBytes(this SecureString obj)
{
return new SecureString[1] {obj}.GetBytes();
}
/// <summary>
/// Get Bytes from an array of secure string objects
/// </summary>
public static byte[] GetBytes(this SecureString[] obj)
{
if (obj == null)
throw new Exception("GetBytes (SecureString[]) object cannot be null.");
var source = new List<byte[]>();
foreach (var secureString in obj)
if (secureString != null)
using (var secureStringHelper = new SecureStringHelper(secureString))
{
var byteArray = secureStringHelper.ToByteArray();
source.Add(byteArray.CloneTo());
}
var seed = new byte[source.Sum(ba => ba.Length)];
return source.Aggregate(seed, (Current, ba) => Current.Add(ba));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment