Skip to content

Instantly share code, notes, and snippets.

@yuka1984
Created November 11, 2015 16:13
Show Gist options
  • Select an option

  • Save yuka1984/5c1dc234c9a477373113 to your computer and use it in GitHub Desktop.

Select an option

Save yuka1984/5c1dc234c9a477373113 to your computer and use it in GitHub Desktop.
BitConverterExtentionMethod
namespace BitConverterExtention
{
public static class BitConverterExtention
{
public static byte[] ToBytes(this char value)
{
return BitConverter.GetBytes(value);
}
public static byte[] ToBytes(this bool value)
{
return BitConverter.GetBytes(value);
}
public static byte[] ToBytes(this byte value)
{
return new byte[1] {value};
}
public static byte[] ToBytes(this short value)
{
return BitConverter.GetBytes(value);
}
public static byte[] ToBytes(this ushort value)
{
return BitConverter.GetBytes(value);
}
public static byte[] ToBytes(this int value)
{
return BitConverter.GetBytes(value);
}
public static byte[] ToBytes(this uint value)
{
return BitConverter.GetBytes(value);
}
public static byte[] ToBytes(this long value)
{
return BitConverter.GetBytes(value);
}
public static byte[] ToBytes(this ulong value)
{
return BitConverter.GetBytes(value);
}
public static byte[] ToBytes(this float value)
{
return BitConverter.GetBytes(value);
}
public static byte[] ToBytes(this double value)
{
return BitConverter.GetBytes(value);
}
public static bool ToBoolean(this byte[] value, int startIndex)
{
return value[startIndex] != 0;
}
public static char ToChar(this byte[] value, int startIndex)
{
return BitConverter.ToChar(value, startIndex);
}
public static double ToDouble(this byte[] value, int startIndex)
{
return BitConverter.ToDouble(value, startIndex);
}
public static short ToInt16(this byte[] value, int startIndex)
{
return BitConverter.ToInt16(value, startIndex);
}
public static int ToInt32(this byte[] value, int startIndex)
{
return BitConverter.ToInt32(value, startIndex);
}
public static long ToInt64(this byte[] value, int startIndex)
{
return BitConverter.ToInt64(value, startIndex);
}
public static float ToSingle(this byte[] value, int startIndex)
{
return BitConverter.ToSingle(value, startIndex);
}
public static ushort ToUInt16(this byte[] value, int startIndex)
{
return BitConverter.ToUInt16(value, startIndex);
}
public static uint ToUInt32(this byte[] value, int startIndex)
{
return BitConverter.ToUInt32(value, startIndex);
}
public static string ToHexString(this byte[] value)
{
return BitConverter.ToString(value);
}
public static string[] ToHexStrings(this byte[] value)
{
return BitConverter.ToString(value).Split('-');
}
public static string ToHexString(this byte[] value, int startIndex)
{
return BitConverter.ToString(value, startIndex);
}
public static string[] ToHexStrings(this byte[] value, int startIndex)
{
return BitConverter.ToString(value, startIndex).Split('-');
}
public static string ToHexString(this byte[] value, int startIndex, int length)
{
return BitConverter.ToString(value, startIndex, length);
}
public static string[] ToHexStrings(this byte[] value, int startIndex, int length)
{
return BitConverter.ToString(value, startIndex, length).Split('-');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment